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

时间:2017-09-25 12:33:31

标签: python sql django models

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

import base64, zlib

a = base64.b4decode("eJwVwwEJAAAAgKD/ry3B5QAdegQ0")
zlib.decompress(a)
>>>zlib.error: Error -3 while decompressing data: invalid distance too far back

但在执行时我收到此错误“'DefaultConnectionProxy'对象没有属性' getitem '”

追溯:

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

有关如何解决此问题的任何想法?我看到有些人不得不在他们的模型中添加一些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__' ,但它似乎对我不起作用。

1 个答案:

答案 0 :(得分:0)

您好像应该使用connections,而不是connection

from django.db import connections

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