Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting Django with MSSQL server

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
like image 294
user1424311 Avatar asked Apr 15 '17 19:04

user1424311


People also ask

Does Django work with MS SQL Server?

Django has a built-in web server that is used for development purposes. The framework supports several database management systems including Microsoft SQL Server.

Can Python connect to Microsoft SQL Server?

You can connect to a SQL Database using Python on Windows, Linux, or macOS.

Can we connect to database Django?

By default, Django works with SQLite, database and allows configuring for other databases as well. Database connectivity requires all the connection details such as database name, user credentials, hostname drive name etc. To connect with MySQL, django. db.


2 Answers

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.

like image 79
rparent Avatar answered Sep 30 '22 09:09

rparent


Following official django documentation (currently django 3.1) django-mssql-backend should be used. Django-MSSQL-backend django database adapter is a fork of django-pyodbc-azure which:

  • Supports Django 2.2, 3.0
  • Supports Microsoft SQL Server 2008/2008R2, 2012, 2014, 2016, 2017, 2019
  • Compatible with Micosoft ODBC Driver for SQL Server, SQL Server Native Client, and FreeTDS ODBC drivers

Other solutions django-pyodbc-azure, django-sqlserver and django-mssql as of 2020-11 look to be obsolete.

like image 39
Robert Lujo Avatar answered Sep 30 '22 08:09

Robert Lujo