5

I am working on a Django webapp, and for this app I would like to have an online database. It just so happens my faculty grants access to using some Microsoft Azure services, including SQL databases. Now, I would like to connect an Azure SQL database to my Django project, however I cannot get this to work. I can connect to the database through PyCharm's data source view, however using the same parameters in settings.py yields this error:

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x03846270>
Traceback (most recent call last):
File "C:\Users\kairy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\autoreload.py", line 227, in wrapper
fn(*args, **kwargs)
File "C:\Users\kairy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management\commands\runserver.py", line 117, in inner_run
autoreload.raise_last_exception()
File "C:\Users\kairy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\autoreload.py", line 250, in raise_last_exception
six.reraise(*_exception)
File "C:\Users\kairy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\six.py", line 685, in reraise
raise value.with_traceback(tb)
File "C:\Users\kairy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\autoreload.py", line 227, in wrapper
fn(*args, **kwargs)
File "C:\Users\kairy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\kairy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\apps\registry.py", line 108, in populate
app_config.import_models()
File "C:\Users\kairy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\apps\config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "C:\Users\kairy\AppData\Local\Programs\Python\Python36-32\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:\Users\kairy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\auth\models.py", line 4, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "C:\Users\kairy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\auth\base_user.py", line 52, in <module>
class AbstractBaseUser(models.Model):
File "C:\Users\kairy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\base.py", line 124, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "C:\Users\kairy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\base.py", line 330, in add_to_class
value.contribute_to_class(cls, name)
File "C:\Users\kairy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\options.py", line 214, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "C:\Users\kairy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\__init__.py", line 33, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "C:\Users\kairy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\utils.py", line 212, in __getitem__
conn = backend.DatabaseWrapper(db, alias)
File "C:\Users\kairy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\sqlserver_ado\base.py", line 184, in __init__
super(DatabaseWrapper, self).__init__(*args, **kwargs)
File "C:\Users\kairy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\backends\base\base.py", line 96, in __init__
self.client = self.client_class(self)
TypeError: 'NoneType' object is not callable

The Database object in settings.py looks like this (sensitive info edited out):

DATABASES = {
    'default': {
        "ENGINE": "sqlserver_ado",
        'NAME': [databasename],
        "HOST": "tcp:[resourcegroup].database.windows.net",
        "PORT": "1433",
        "USER": [username@resourcegroup],
        "PASSWORD": [password]
    }
}

What is wrong with this configuration?

Troels Jessen
  • 125
  • 1
  • 10
  • which version of django you are using? and is this the command to install `pip install django-pyodbc-azure`? – Tiny.D May 02 '17 at 07:06
  • The content of your `settings.py` doesn't look like the [example](https://github.com/michiya/django-pyodbc-azure#example) of `django-pyodbc-azure`. Please refer to the example to change your configuration, such as `HOST` without `tcp:`, the `[resourcegroup]` should be the server host name which you can see on Azure portal. Any concern or update, please feel free to let me know. – Peter Pan May 02 '17 at 09:55

1 Answers1

7

Per my experience, if you want to connect Azure SQL Database from Django webapp, the simple way of best practice is that follow the django-pyodbc-azure README to install python packages pyodbc & django-pyodbc-azure and configure the settings.py file of Django correctly as below.

DATABASES = {
    'default': {
        'ENGINE': 'sql_server.pyodbc',
        'NAME': 'mydb',
        'USER': 'user@myserver',
        'PASSWORD': 'password',
        'HOST': 'myserver.database.windows.net',
        'PORT': '',
        'OPTIONS': {
            'driver': 'ODBC Driver 13 for SQL Server',
        },
    },
}
Peter Pan
  • 23,476
  • 4
  • 25
  • 43
  • We decided to just keep using the SQLite database provided by default, since the software was more proof-of-concept anyways, but I was definitely using the wrong database connector, as I was attempting to use the one for MSSQL databases. Thanks for the help! – Troels Jessen May 30 '17 at 10:04
  • The pacjage mentioned supports a short term supported django version – emanuel sanga Jan 20 '21 at 17:41
  • 1
    Please note that django-mssql-backend is a maintained fork of the above mentioned package. – ic_fl2 Sep 17 '21 at 11:59
  • This seems to be a rapidly changing topic. `django-pyodbc-azure` will not work for DB migrations, solution is to use `mssql-django`. See: https://stackoverflow.com/a/68985493/1548275 – Jari Turkia Jul 16 '23 at 16:19