调用存储过程时出现错误“'DefaultConnectionProxy' 对象没有属性 '__getitem__'”

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

您好,我正在使用 python/Django 创建一个项目,在我的第一页(索引)上,我正在调用一个存储过程来获取数据。

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())

但是在执行时我得到这个错误“'DefaultConnectionProxy' object has no attribute 'getitem'”

回溯:

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-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__'

知道如何解决这个问题吗?我看到有些人不得不在他们的模型中添加一些 def __unicode__ 但它似乎对我不起作用。

看起来你应该使用 connections,而不是 connection

from django.db import connections

cursor = connections['site'].cursor()