Search code examples
pythonsql-serverdjangodjango-mssql

Error connecting Django 2.0 with sql server 2014 using python 3.6


I am new in Django and I am trying to connect it with Sql server. As I read there is a third party connector for this.

Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "C:\Program Files\Python36\lib\site-packages\django\core\management\__init__.py", line 371, in execute_from_command_line
utility.execute()
File "C:\Program Files\Python36\lib\site-packages\django\core\management\__init__.py", line 347, in execute
django.setup()
File "C:\Program Files\Python36\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Program Files\Python36\lib\site-packages\django\apps\registry.py", line 112, in populate
app_config.import_models()
File "C:\Program Files\Python36\lib\site-packages\django\apps\config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "C:\Program Files\Python36\lib\importlib\__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 Files\Python36\lib\site-packages\django\contrib\auth\models.py", line 2, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "C:\Program Files\Python36\lib\site-packages\django\contrib\auth\base_user.py", line 47, in <module>
class AbstractBaseUser(models.Model):
File "C:\Program Files\Python36\lib\site-packages\django\db\models\base.py", line 114, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "C:\Program Files\Python36\lib\site-packages\django\db\models\base.py", line 315, in add_to_class
value.contribute_to_class(cls, name)
File "C:\Program Files\Python36\lib\site-packages\django\db\models\options.py", line 205, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "C:\Program Files\Python36\lib\site-packages\django\db\__init__.py", line 33, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "C:\Program Files\Python36\lib\site-packages\django\db\utils.py", line 202, in __getitem__
backend = load_backend(db['ENGINE'])
File "C:\Program Files\Python36\lib\site-packages\django\db\utils.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'

I have seen many posts 1,2,3,4 here in SOF but none seem to work to me. I dont want to downgrade my version of Django or python in order to make things work.
I tried to copy to sqlserver_ado folder to

C:\Program Files\Python36\Lib\site-packages\django\db\backends but didnt seem to work. I am sure there is a workaround for this. I am using Django 2.0 and python 3.6.2

EDIT 1

I restarted my pc and upgraded to Django 2.0.1 . Now the error is

File "C:\Program Files\Python36\lib\site-
packages\django\db\backends\sqlserver_ado\schema.py", line 9, in <module>
from django.utils.text import force_text
ImportError: cannot import name 'force_text'

I needed to put

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

without the django.db.backends it wouldnt recoginze it. I bet there are some compatibilities issues with these versions of python django and django-mssql...


Solution

  • Found the solution and post it if anyone facing same problem.

    As the time of writing, django-mssql supports up to version 1.8 of django.

    Use django-pyodbc-azure instead of django-mssql

    The settings that worked for me:

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