'pyodbc.Cursor' object has no attribute 'callproc', mssql with django

29 Views Asked by At

I cannot execute a procedure stored on mssql server from django.
Here is the configuration for the db.

DATABASES = {
    'default': {
        'ENGINE': 'mssql',
        'NAME': os.environ.get('DB_NAME'),
        'USER': os.environ.get('DB_USER'),
        'PASSWORD': os.environ.get('DB_PASSWORD'),
        'HOST': os.environ.get('DB_HOST'),
        'OPTION':{
            'driver': 'ODBC Driver 17 for SQL Server',
            'trusted_connection': 'yes'
        }
    }
}

In my view:

def test(request):
    if request.method == "POST":
        with connection.cursor() as cursor:
            P1 = request.data["p1"]
            P2 = request.data["p2"]
            try:
                cursor.callproc('[dbo].[SP_TEXT]',[P1,P2])
                if cursor.return_value == 1:
                    result_set = cursor.fetchall()
                    print(result_set)
            finally:
                cursor.close()
            return Response({"msg":"post"})
    else:
        return Response({"msg": "get"})
0

There are 0 best solutions below