Django and MS SQL Server 2012 Connection (2018)

Royce Chua
4 min readMay 19, 2018

--

As of 2018 and the recent Django 2.0 documentation, the MS SQL server engine is still considered as a 3rd party back-end SQL engine. But the good news is that Microsoft has supported the Django + MS SQL engine back-end by developing a driver based on the pyodbc.

The new python module/library is the

Which supports Django 2.0 and lower versions like Django 1.11.

Feel free to visit the Github page for more information (installation instructions as well).
Link: https://github.com/michiya/django-pyodbc-azure

This Python module be used to connect to both local and azure based MS SQL Servers and Databases. This is the module we are going to use today in our Django 2.0 sample project.

Before continuing, make sure you have installed the MS SQL Server from Microsoft. SQL Server is what we will use to create SQL Server databases and execute queries in.

Visit the link: https://www.microsoft.com/en-ph/download/details.aspx?id=29062

Click Download, then choose from any of these 3 options, I personally chose the EXPRADV_x64_ENU.exe for a more complete set of tools. A 32-bit user will need to scroll down and see the same set of choices but it is in x86 instead.

To get a more comprehensive SQL Server 2012 installation visit this YouTube link: https://www.youtube.com/watch?v=4WEFTQ3VJNg

Make sure that you have the SQL Server 2012 Management Studio.

Note: Make sure to be in the correct Python environment you want django-pyodbc-azure to be installed to. Type the command below.

pip install django-pyodbc-azure

Consult the Github page for more information on other versions 1.11 and below.

After completing the pip installation and have the Management studio installed. Create a database in the SQL Server Management Studio.

I’m gonna name my database DjangoDBTest as a sample.

If you haven’t already, create a Django project where you can connect the MS SQL Server to. Then go to the Settings.py inside your project folder.

Look for the DATABASES={….} and alter the code to match format below.

DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': '<DB NAME>',
'HOST': '<HOST IP>',
'USER': '<SQL SERVER USERNAME',
'PASSWORD': '<SQL SERVER PASSWORD>',

'OPTIONS': {
'driver': 'ODBC Driver 13 for SQL Server',
}
}
}

Continue down below if you don’t know what values to place in the < >.

‘sql_server.pyodbc’ — is the name of the MS SQL Database Engine.

‘DjangoDBTest’ — is the name of the SQL database we created earlier. (Django doesn’t automatically create a database for you, it only connects to a DB.)

In the ‘HOST’, the values can be any IP Address where the SQL server is hosted on.
If in my case you are using the localhost you get the host value here.

‘USER’ and ‘PASSWORD’ is dependent on what you have configured on your SQL Server and not your Django project. Right click on Logins and select new login if you want to create a new user.

‘OPTIONS’ — Specifies which type of SQL Server driver is going to be used. You can choose from the official pyodbc documentation below.

NOTE: If you are having an error when trying to run the Django project. Try these steps

NOTE: If you are having an error when trying to run the Django project. Try these steps

  1. Try using ” ” like the example below
    ‘OPTIONS’: {“ODBC Driver 13 for SQL Server”, }
    Some errors are caused by the string parsing function of the pyodbc library.
  2. Check in python shell which pyodbc driver’s are currently installed in your computer by opening python shell and typing the following commands:
    >> import pyodbc
    >> pyodbc.drivers()
  3. Install the necessary driver’s according to the image below.

Link: https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-SQL-Server-from-Windows

After the setup. Try running the command python manage.py runserver, there should be no errors.

Finally run python manage.py migrate to ensure that the connection was successful.

Check the Management Studio to verify the Migration.

Now, you’re all setup to develop Django Web Applications with MS SQL Server 2012.

If you found the tutorial helpful please like and share this article, Thanks a lot!

Originally published at formerly https://rctechblogs.wordpress.com/ now https://techincommons.wordpress.com/2018/05/19/django-and-ms-sql-server-2012-connection-2018/on May 19, 2018.

--

--