使用 python 3.6 将 Django 2.0 与 SQL Server 2014 连接时出错



我是Django的新手,我正在尝试将其与SQL服务器连接。正如我所读到的,有一个第三方连接器。

Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "C:Program FilesPython36libsite-packagesdjangocoremanagement__init__.py", line 371, in execute_from_command_line
utility.execute()
File "C:Program FilesPython36libsite-packagesdjangocoremanagement__init__.py", line 347, in execute
django.setup()
File "C:Program FilesPython36libsite-packagesdjango__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:Program FilesPython36libsite-packagesdjangoappsregistry.py", line 112, in populate
app_config.import_models()
File "C:Program FilesPython36libsite-packagesdjangoappsconfig.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "C:Program FilesPython36libimportlib__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 978, in _gcd_import
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "C:Program FilesPython36libsite-packagesdjangocontribauthmodels.py", line 2, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "C:Program FilesPython36libsite-packagesdjangocontribauthbase_user.py", line 47, in <module>
class AbstractBaseUser(models.Model):
File "C:Program FilesPython36libsite-packagesdjangodbmodelsbase.py", line 114, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "C:Program FilesPython36libsite-packagesdjangodbmodelsbase.py", line 315, in add_to_class
value.contribute_to_class(cls, name)
File "C:Program FilesPython36libsite-packagesdjangodbmodelsoptions.py", line 205, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "C:Program FilesPython36libsite-packagesdjangodb__init__.py", line 33, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "C:Program FilesPython36libsite-packagesdjangodbutils.py", line 202, in __getitem__
backend = load_backend(db['ENGINE'])
File "C:Program FilesPython36libsite-packagesdjangodbutils.py", line 125, in load_backend
) from e_user
django.core.exceptions.ImproperlyConfigured: 'sqlserver_ado' isn't an available database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
'mysql', 'oracle', 'postgresql', 'sqlite3', 'sqlserver_ado'

我在 SOF 中看到了很多帖子 1,2,3,4,但似乎没有一个对我有用。我不想降级我的Django或python版本以使事情正常工作。
我试图复制到sqlserver_ado文件夹

C:\Program Files\Python36\Lib\site-packages\django\db\backends,但似乎不起作用。我相信有一个解决方法。我正在使用Django 2.0和python 3.6.2

编辑 1

我重新启动了我的电脑并升级到 Django 2.0.1 .现在错误是

File "C:Program FilesPython36libsite-
packagesdjangodbbackendssqlserver_adoschema.py", line 9, in <module>
from django.utils.text import force_text
ImportError: cannot import name 'force_text'

我需要把

 DATABASES = {
    'default': {
        'NAME': 'DjangoTutorial',
        'ENGINE': 'django.db.backends.sqlserver_ado',
        'HOST': 'localhost',
        'USER': 'whateveruser',
        'PASSWORD': 'whateverpass',
    }
}

如果没有 Django.db.后端,它就不会重新识别它。我敢打赌,这些版本的python django和django-mssql存在一些兼容性问题。

找到解决方案并在有人遇到相同问题时发布。

在撰写本文时,django-mssql 最多支持 django 的 1.8 版本。

使用 django-pyodbc-azure 而不是 django-mssql

对我有用的设置:

DATABASES = {
    'default': {
        'NAME': 'DjangoTutorial',
        'ENGINE': 'sql_server.pyodbc',
        'HOST': 'localhost',
        'USER': 'whateveryouuser',
        'PASSWORD': 'whateveryouuser',
        'PORT': '',
        'OPTIONS': {
            'driver': 'SQL Server Native Client 11.0',
        },
    }
}

此配置仅适用于 Django 1.8。希望这有帮助。

DATABASES = {
'default': {
    'ENGINE': 'sqlserver_ado',
    'NAME': '',
    'USER': '',
    'PASSWORD': '',
    'HOST': '',
    'PORT': '1433',
     'OPTIONS': {
        'provider': 'SQLOLEDB', #SQLNCLI11 , SQLOLEDB
        'use_legacy_date_fields': 'True',
        #'extra_params' : 'DataTypeCompatibility=80;MARS Connection=True',
        #'connect_timeout': 0
        }
}
}

相关内容

  • 没有找到相关文章

最新更新