pyodbcpython-3.7django-pyodbcdjango-pyodbc-azuredjango-2.1

Django 2.1 SQL Server 17 error - isn't an available database backend. Try using 'django.db.backends.XXX'


django.core.exceptions.ImproperlyConfigured: 'django-pyodbc-azure' isn't an available database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
    'mysql', 'oracle', 'postgresql', 'sqlite3'

I have tried switching to the following DATABASE settings, with no luck.

DATABASES = {
    'default': {
        'ENGINE': 'django-pyodbc-azure',  #None of these work either: 'django_pyodbc' 'sqlserver_ado', 'sql_server.pyodbc', 'django-pyodbc-azure','pyodbc'
        'HOST': 'test-server-local',
        'NAME': 'db-name',
        'USER': 'sql_username',
        'PASSWORD': 'password',
        'PORT': '1433'
            # 'OPTIONS': {
            # 'driver': 'ODBC Driver 17 for SQL Server'
            # 'driver': 'SQL Server Native Client 11.0',
            # 'provider': 'SQLOLEDB' # Have also tried 'SQLCLI11' and 'SQLOLEDB'
            # 'extra_params': 'DataTypeCompatibility=80'
        # }
    }
}

I was able to hit a SQLLite database, and am able to run a pyodbc import (directly via Python) but am not able to use this in Django.

I referred to the following questions on stack overflow already (with no luck). Solution 1 Solution 2 and the Django/SQL Server versions they have in place are quite older than what I'm set up with. I wouldn't want to downgrade from Django v2.1 to a lower one, just to support the SQL Server database without checking if there are other options.

I verified and added the following site-packages into my environment/system path variables:

    C:\Users\\AppData\Local\Programs\Python\Python37-32\Lib\site-packages
    - django_pyodbc
    - django_pyodbc-2.0.0a1.dist-info
    - django_mssql-1.8.dist-info
    - django_pyodbc_azure-2.1.0.0.dist-info

Running the following code does hit the database and get data back, so I know it's not just Python, but probably Django.

import pyodbc 
server = 'test-server-local' 
database = 'TEST-DB'
username = 'sql_username'
password = 'password'
cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()

cursor.execute("SELECT DepartmentName, DepartmentID from dbo.Departments;") 
row = cursor.fetchone() 
while row: 
    print(row.DepartmentID, row.DepartmentName)
    row = cursor.fetchone()

Solution

  • Here's a full setup that is verified as working, as long as django-pyodbc-azure is installed in your current virtualenv:

    DATABASES = {
        'default': {
            'ENGINE': 'sql_server.pyodbc',
            'HOST': 'dbserver.your-domain.com',
            'PORT': '1433',
            'NAME': 'project',
            'USER': 'project_user',
            'PASSWORD': 'project_password',
            'OPTIONS': {
                'driver': 'ODBC Driver 17 for SQL Server',
                'unicode_results': True,
            },
        },
    }
    

    Full instructions are here: https://pyphilly.org/django-and-sql-server-2018-edition/