Use Django pyodbc SQLSERVER issue

5.4k views Asked by At

I'm trying to connect django with sqlserver . I already installed python odbc and django-odbc.

My data dabase configuration (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.
    }
}

However, I'm getting this error when I try to run the server:

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'

Can someone help me to figure out how to fix it?

7

There are 7 answers

0
Rob Hay On BEST ANSWER

Bro,

this is 2 years old now... hope you have an answer. For what its worth: The Database object you're declaring needs an 'options' param, see below.

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

1
Daniel Roseman On

Looks like django-odbc is a dead project and is no longer compatible with the latest Django, since version 1.3.

You could potentially fix it by editing C:\Python27\lib\site-packages\sql_server\pyodbc\base.py", line 56 to look at settings.DATABASES['default'].get('options'), but if you do you'll no doubt uncover more incompatibilities further down the line.

0
samrand sheikhmarufi On

Remove sql server folders in Lib\site-packages\ so execute pip install django-pyodbc-azure pip install sql_server.pyodbc

0
Michael Baltaks On

Looks like someone has picked up django-pyodbc for django 1.4 here https://github.com/avidal/django-pyodbc

0
Dan Green On

I got a bit further by switching to the branch mentioned by Michael Baltaks. I used the following commands to uninstall the old incompatible version:

$ pip uninstall sql-server.pyodbc

and then install the git-hub version:

$ pip install https://github.com/avidal/django-pyodbc/archive/django-1.4.zip

This however did not get me all the way to working. I now get the following error:

pyodbc.Error: ('00000', '[00000] [iODBC][Driver Manager]dlopen({FreeTDS}, 6): image not found (0) (SQLDriverConnect)')

0
Miguel Febres On

Take a look on this fork: https://github.com/avidal/django-pyodbc

It is compatible with Django 1.4

0
Dasith On

This worked for me, specifically the driver_supports_utf8 line:

'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,
            }
        }