Search code examples
pythonsqldjangomodels

Error "'DefaultConnectionProxy' object has no attribute '__getitem__'" when calling stored procedure


Hi i'm making a project using python/Django, on my first page (index) i'm calling a stored procedure to fetch the data.

def index(request):
    listOPT = []
    Data = []
    fluxState = settings.FLUX_MANAGER_STATE
    if fluxState != True:
        listOPT.append('FLUX AXEREAL : OFF')
    cursor = connection['site'].cursor()
    """MS SQL CALL TO STORED PROCEDURE SP_webGET_RESUME_MVT_INIT_ACTIF """
    cursor.execute("{CALL SP_webGET_RESUME_MVT_INIT_ACTIF}")
    mouvements = cursor.fetchall()
    cursor.close()
    return render(request, 'index.html', locals())

but when executing i get this error "'DefaultConnectionProxy' object has no attribute 'getitem'"

Traceback :

Environment:


Request Method: GET
Request URL: http://10.1.20.14:8084/gestion_mouvement/index

Django Version: 1.9.6
Python Version: 2.7.11
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'Gestion_Mouvement']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'easy_pjax.middleware.UnpjaxMiddleware']



Traceback:

File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "E:\prog2\PJ1705-027_Automatisation_Silo1_Site_LADON\4-Developpement\Informatique\Web\Gestion_Mouvements\Gestion_Mouvements\Gestion_Mouvement\views.py" in index
  26.     cursor = connection['site'].cursor()
Exception Type: TypeError at /gestion_mouvement/index
Exception Value: 'DefaultConnectionProxy' object has no attribute '__getitem__'

Any idea on how to fix this ? i saw some people had to add some def __unicode__ to their models but it doesn't seem to work for me.


Solution

  • It looks like you should be using connections, not connection.

    from django.db import connections
    
    cursor = connections['site'].cursor()