8

I wanted to use SQL Server as the backend for Django, but I got this when debugging the web project. 'sql_server.pyodbc' isn't an available database backend. Error was: No module named sql_server.pyodbc.base.

Python Environments (Python 2.7) with Django (1.7), pyodbc(3.0.10), pywin32(218.3). And here is my settings.py:

DATABASES = {
'default': {
    'ENGINE': 'sql_server.pyodbc',
    'NAME': 'DatabaseName',
    'USER': 'user',
    'PASSWORD': 'pwd',
    'HOST': '127.0.0.1',
    'PORT': '',
    'OPTIONS': {
        'driver': 'SQL Server Native Client 11.0',
        'server': 'ServerName',
        'MARS_Connection': True,
        'dsn': 'MSSQL-PYTHON',
        },
    }
}
Weihui Guo
  • 3,669
  • 5
  • 34
  • 56

2 Answers2

18

You have not installed the package with the required DB backend.

Do:

pip install django-pyodbc
pip install django-pyodbc-azure

See this doc and this one.

An example of the database settings from the second link:

DATABASES = {
'default': {
    'ENGINE': 'sql_server.pyodbc',
    'NAME': 'mydb',
    'USER': 'user@myserver',
    'PASSWORD': 'password',
    'HOST': 'myserver.database.windows.net',
    'PORT': '',

    'OPTIONS': {
        'driver': 'SQL Server Native Client 11.0',
    },
  },
}

#set this to `False` if you want to turn off pyodbc's connection pooling:
DATABASE_CONNECTION_POOLING = False
Ivan
  • 5,803
  • 2
  • 29
  • 46
  • 1
    I installed django-pyodbc as you suggested, although there is a pyodbc already. It gave me the same error. – Weihui Guo Sep 23 '15 at 17:23
  • 1
    It seems still like you haven't got the right backend package. Did you try [this one](https://pypi.python.org/pypi/django-pyodbc-azure/1.8.3.0) ? – Ivan Sep 23 '15 at 17:54
  • The link you gave about django-pyodbc-azure 1.8.3.0 actually solved the problem. Somehow when I installed it, it also updated my Django to 1.8.4. So I guess matching the version really matters. Please update your answer so I can accept it. Thank you very much! – Weihui Guo Sep 23 '15 at 19:58
  • 1
    It seems that django-pyodbc-azure (1.8.3.0) only provides sql_server.pyodbc.base. If I remove pyodbc and leave django-pyodbc-azure alone, it gives me different error. I need both pyodbc (3.0.10) and django-pyodbc-azure (1.8.3.0) – Weihui Guo Sep 23 '15 at 20:11
  • If you don't want to update Django 1.9 to 1.10, do not install the latest version of django-pyodbc-azure. Here are the versions I installed: pyodbc==3.0.10, django-pyodbc==1.0.1, django-pyodbc-azure==1.9.6 ... Cheers! – joaorodr84 Jan 12 '17 at 19:26
  • Future readers: under Linux I kept getting errors like `error: command 'x86_64-linux-gnu-gcc' failed with exit status 1` and had to `apt-get install unixodbc-dev` before `pip install django-pyodbc` to work. – Dinei May 24 '17 at 17:52
0

Take a look at this link:

DATABASES = {
    'default': {
        'NAME': 'my_database',
        'ENGINE': 'sqlserver_ado',
        'HOST': 'dbserver\\ss2008',
        'USER': '',
        'PASSWORD': '',
    }
}

Supposedly you can use SQL Server with Django MSSQL (link above). you might want to check the [Django documentation] to see what other database support django supports "natively". (https://docs.djangoproject.com/en/1.8/ref/settings/#databases)

jlnabais
  • 829
  • 1
  • 6
  • 18