Connecting Django with MSSQL server

和自甴很熟 提交于 2019-12-02 01:47:34

问题


I'm trying to connect my Django app to SQL Server 2016. I've tried using django-pyodbc but it doesn't support Django 1.11. Instead I installed django-mssql 1.8. When I try to run the application I get this error.

TypeError was unhandled by user code
Message: 'NoneType' object is not callable

At execute_from_command_line(sys.argv) in manage.py

Here is my DATABASES from settings.py

DATABASES = {
'default': {
    'ENGINE': 'sqlserver_ado',
    'NAME': 'TEST2',
    'HOST': 'PCNAME\SQLEXPRESS',
    'USER': '',
    'PASSWORD': '',
    'OPTIONS' : {
     'provider': 'SQLOLEDB',
     'use_mars': True,
     },
}
}

I've tried both the default and SQLOLEDB provider but always get the same error. I've also tried with and without user and password set but the error remains the same. I am able to connect to a local MySQL DB just fine.

I'm running Windows 10, Visual Studio 2015, SQL Server Express 2016

Edit:

Here's the output from pip freeze

appdirs==1.4.3
Django==1.11
django-mssql==1.8
mysqlclient==1.3.10
packaging==16.8
pyodbc==4.0.16
pyparsing==2.2.0
pytz==2017.2
six==1.10.0

Here's my requirements.txt

django==1.11
mysqlclient==1.3.10
django-mssql==1.8

回答1:


As stated in the django-mssql documentation, the latest release only supports Django 1.8, so it won't work with Django 1.11.

You will have to wait until the package supports newer versions of django to upgrade. That is the problem when using Django with non-supported database backends, you depend on the third party packages maintenance, and this one seems to have trouble staying up to date with Django.




回答2:


You can use django-pyodbc-azure because it has support up to current versions of django 2.0. After installation you need to edit in your settings file like below:

DATABASES = {
    'default': {
        'ENGINE': 'sql_server.pyodbc',
        'NAME': DB_NAME,
        'USER': USER,
        'PASSWORD': PASSWORD,
        'HOST': HOST,
        'PORT': PORT,
        'OPTIONS': {
            'driver': 'ODBC Driver 13 for SQL Server',
            'unicode_results': True,

        },
    }
}

If you install TDS library as driver then your driver will be 'driver':'Free TDS' Here 13 is the default version. If your installed version is different from that then use that version number instead of 13



来源:https://stackoverflow.com/questions/43430091/connecting-django-with-mssql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!