4

I'm having some issues setting up django-mssql on Win Server 2008 R2. I have everything installed, however, the wiki for django-mssql says to setup the settings file similar to:

DATABASES = {
'default': {
    'NAME': 'my_database',
    'ENGINE': 'sqlserver_ado',
    'HOST': 'dbserver\\ss2008',
    'USER': '',
    'PASSWORD': '',
    'OPTIONS' : {
        'provider': 'SQLOLEDB',
        'use_mars': True,
    },
   }
}

When I run from my site directory:

 python manage.py syncdb

I get an error stating it isn't an available database backend. When I installed django-mssql it seemed to install the backend here \site-packages\django_mssql-1.0.1-py2.7.egg\sqlserver_ado does this need to be copied to site-packages\django\db\backends?

I get the same error if I set my settings to:

DATABASES = {
'default': {
    'NAME': 'my_database',
    'ENGINE': 'django_mssql-1.0.1-py2.7.egg.sqlserver_ado',
    'HOST': 'dbserver\\ss2008',
    'USER': '',
    'PASSWORD': '',
    'OPTIONS' : {
        'provider': 'SQLOLEDB',
        'use_mars': True,
    },
   }
}

Am I missing something when setting up this backend? This is my first time using django, but I didn't see anything in the documentation for setting up a different backend, and the django-mssql wiki or issues doesn't seem to have anything either.

Also, if there is other documentation somewhere that can help please let me know.

EDIT: The django app is running on Ubuntu server.

rjbez
  • 802
  • 2
  • 12
  • 21
  • I would not copy external files into your Django directory. Not a good idea. But you probaly need to import the backend somewhere in your settings files. Maybe try `from mssql import *`? – j_syk Mar 30 '12 at 16:25
  • When you decide to upgrade to Django 1.4, you'll need to update to django-mssql v1.1. `pip install django-mssql==1.1` – Manfre Jun 13 '12 at 20:24
  • Thanks, glad they updated it to work with 1.4 – rjbez Jun 14 '12 at 16:02

6 Answers6

6

Dustin's comment about making sure "import sqlserver_ado" from the command shell got me going down the right path on my Django 1.8.1, Python 3.5 Win32 system with pywin32 installed.

SPOILER ALERT This only gets my Django instance to run without errors. I haven't tested the ADO connection yet.

The first error message I got was:

No module named 'django.db.backends.util'

and I found there is a file called: django.db.backends.utils so I copied it and renamed it to django.db.backends.util (without the 's') and away went the error message!

So hoping this wasn't too harmful, I continued on this line of troubleshooting.

The next error message I got was:

  File "C:\Program Files (x86)\Python35-32\lib\site-packages\sqlserver_ado\base.py", line 7, in <module>
from django.db.backends import BaseDatabaseWrapper, BaseDatabaseFeatures, BaseDatabaseValidation, BaseDatabaseClient 
ImportError: cannot import name 'BaseDatabaseWrapper'

I changed line 7 in base.py to now say:

#from django.db.backends import BaseDatabaseWrapper, BaseDatabaseFeatures, BaseDatabaseValidation, BaseDatabaseClient
from django.db.backends.base.base import BaseDatabaseWrapper
from django.db.backends.base.features import BaseDatabaseFeatures
from django.db.backends.base.validation import BaseDatabaseValidation
from django.db.backends.base.client import BaseDatabaseClient

Yes, that's commenting out the bad line and adding four separate lines. Then I got this error:

  File "C:\Program Files (x86)\Python35-32\lib\site-packages\sqlserver_ado\base.py", line 18, in <module>
from .introspection import DatabaseIntrospection

File "C:\Program Files (x86)\Python35-32\lib\site-packages\sqlserver_ado\introspection.py", line 3, in from django.db.backends import BaseDatabaseIntrospection ImportError: cannot import name 'BaseDatabaseIntrospection'

so I changed the line 3 to now read:

from django.db.backends.base.introspection import BaseDatabaseIntrospection

and so on for creation.py:

from django.db.backends.base.creation import BaseDatabaseCreation

for operations.py:

from django.db.backends.base.operations import BaseDatabaseOperations

for schema.py:

from django.utils.log import getLogger

Hope this helps someone. Hope the ADO module actually connects to something.

-Sean

Seansms
  • 61
  • 1
  • 3
5

You will want to make sure that you can import "sqlserver_ado" from your python shell.

Put the folder sqlserver_ado somewhere on your PATH, I put mine in \site-packages\

Take a look at the README.txt.

The engine does want to be set to "sqlserver_ado" similar to how the settings are done on the settings sample page.

Dustin Holden
  • 180
  • 2
  • 12
  • This seemed to do it...the documentation I had didn't have that tid-bit. Thanks. – rjbez Apr 26 '12 at 15:52
  • One other thing for to consider is update to pywin32. See http://stackoverflow.com/questions/4145079/importerror-no-module-named-pythoncom – Dan Doyon Jun 22 '12 at 17:26
3

As of 2019:

I couldn't get Django MSSQL to work at all.

I switched over to django-pyodbc-azure and that works great.

Install:

pip install django-pyodbc-azure

Setup:

'ENGINE': 'sql_server.pyodbc'
Dustin Michels
  • 2,951
  • 2
  • 19
  • 31
1

You need to install the dependency PyWin32. You can install via pip or download from the python binaries page http://www.lfd.uci.edu/~gohlke/pythonlibs/

jugovich
  • 123
  • 2
  • 8
  • 1
    Sorry, this wasn't mentioned above but the django app is running on Ubuntu. So the PyWin32 wont install. – rjbez Mar 03 '14 at 15:43
1

I was trying to get django_pyodbc to work, and couldn't. I was getting this error:

django.core.exceptions.ImproperlyConfigured: 'django_pyodbc' isn't an available database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
    u'base', u'mysql', u'oracle', u'postgresql_psycopg2', u'sqlite3'
Error was: cannot import name BaseDatabaseWrapper

I was directed to this post as a solution, so I'll post my answer here also. I downgraded to django 1.6 instead of 1.8, and now django_pyodbc works as a database backend.

Community
  • 1
  • 1
isaaclw
  • 923
  • 9
  • 22
0

As per https://github.com/lionheart/django-pyodbc/pull/96, django_pyodbc should now work with Django 1.8. So this seems to be a good alternative to django-mssql for those requiring SQL Server 2008 R2 support.

Andrew Basile
  • 866
  • 8
  • 8