使用Django pyodbc SQLSERVER问题

时间:2012-06-01 13:30:07

标签: django pyodbc django-pyodbc

我正在尝试将django与sqlserver连接。我已经安装了python odbc和django-odbc。

我的数据数据库配置(settings.py)

DATABASES = {
    'default': {
        'ENGINE': 'sql_server.pyodbc', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'EDAS',                      # Or path to database file if using sqlite3.
        'USER': 'sa',                      # Not used with sqlite3.
        'PASSWORD': '1324',                  # Not used with sqlite3.
        'HOST': 'DBIO01-HP',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '1433'                      # Set to empty string for default. Not used with sqlite3.
    }
}

但是,当我尝试运行服务器时,我收到此错误:

C:\ edas> python manage.py runserver

Validating models...

Unhandled exception in thread started by <bound method Command.inner_run of <django.contrib.staticfiles.management.comma
nds.runserver.Command object at 0x02EC2E70>>
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\django\core\management\commands\runserver.py", line 91, in inner_run
    self.validate(display_num_errors=True)
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 266, in validate
    num_errors = get_validation_errors(s, app)
  File "C:\Python27\lib\site-packages\django\core\management\validation.py", line 23, in get_validation_errors
    from django.db import models, connection
  File "C:\Python27\lib\site-packages\django\db\__init__.py", line 40, in <module>
    backend = load_backend(connection.settings_dict['ENGINE'])
  File "C:\Python27\lib\site-packages\django\db\__init__.py", line 34, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "C:\Python27\lib\site-packages\django\db\utils.py", line 92, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "C:\Python27\lib\site-packages\django\db\utils.py", line 24, in load_backend
    return import_module('.base', backend_name)
  File "C:\Python27\lib\site-packages\django\utils\importlib.py", line 35, in import_module
    __import__(name)
  File "C:\Python27\lib\site-packages\sql_server\pyodbc\base.py", line 56, in <module>
    elif 'collation' in settings.DATABASE_OPTIONS:
  File "C:\Python27\lib\site-packages\django\utils\functional.py", line 185, in inner
    return func(self._wrapped, *args)
AttributeError: 'Settings' object has no attribute 'DATABASE_OPTIONS'

有人可以帮我弄清楚如何修复它吗?

7 个答案:

答案 0 :(得分:3)

通过切换到Michael Baltaks提到的分支,我得到了更多。我使用以下命令卸载旧的不兼容版本:

$ pip uninstall sql-server.pyodbc

然后安装git-hub版本:

$ pip install https://github.com/avidal/django-pyodbc/archive/django-1.4.zip
然而,这并没有让我一路走来工作。我现在收到以下错误:

  

pyodbc.Error:('00000','[0000] [iODBC] [Driver Manager] dlopen({FreeTDS},6):找不到图像(0)(SQLDriverConnect)')

答案 1 :(得分:1)

看起来有人在这里为django 1.4选择了django-pyodbc https://github.com/avidal/django-pyodbc

答案 2 :(得分:0)

看起来django-odbc是一个死项目,并且自1.3版以来不再与最新的Django兼容。

您可以通过编辑C:\ Python27 \ lib \ site-packages \ sql_server \ pyodbc \ base.py“,第56行查看settings.DATABASES['default'].get('options')来修复它,但如果你这样做,你将毫无疑问发现更多的不兼容性。

答案 3 :(得分:0)

看看这个分叉:https://github.com/avidal/django-pyodbc

它与Django 1.4兼容

答案 4 :(得分:0)

兄弟,

现在已经2岁了...希望你有答案。 它的价值:你声明的数据库对象需要一个'选项'参数,见下文。

#Database connector
DATABASES = {
    'default': {
        'ENGINE': '',
        'NAME': '',
        'USER': '',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
        'OPTIONS': {
            'driver': '',
            'MARS_Connection': True,
        },
    }
}

答案 5 :(得分:0)

这对我有用,特别是driver_supports_utf8行:

'dbconn': {
            'CREATE_DB': False,
            'CREATE_USER': False,
            'CREATE_TBLSPACE': False,
            'ENGINE': 'django_pyodbc',
            'NAME': 'DBNAME',
            'USER': 'user',
            'PASSWORD': 'password',
            'HOST': 'HOST',
            'OPTIONS': {
                'host_is_server': True,
                'driver_supports_utf8': True,
            }
        }

答案 6 :(得分:0)

删除Lib\site-packages\中的sql server文件夹,因此执行 pip安装django-pyodbc-azure pip安装sql_server.pyodbc