Setting up django-mssql issues

别来无恙 提交于 2019-11-26 17:17:18

问题


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.


回答1:


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.




回答2:


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




回答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'



回答4:


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/




回答5:


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.




回答6:


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.



来源:https://stackoverflow.com/questions/9944204/setting-up-django-mssql-issues

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