Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Django with Sql Server

I normally use Postgres for my database needs with Django but I recently started at a company which use MSSQL on a Windows environment. Long story short I had to rewrite the database properties in settings.py. Unfortunately, I have NO idea how to connect to a SQL Server using Pyodbc and they're running Python 3.x so I can't use Django-Pyodbc. While trying to run it I'm getting a: "Data source name not found and no default driver specified (0) (SQLDriverConnect)')"

Here is my current db config as it stands. I'm probably doing something wrong but it is very difficult to find resources since most Django+Sql Server results either use FreeTDS or Django-Pyodbc (neither are options).

'default': {
    'ENGINE': 'sql_server.pyodbc',
    'NAME': 'db_name_on_server',
    'USER': 'my_acct',
    'PASSWORD': 'nope',
    'HOST': 'x.x.x.x',
    'PORT': '1433',
    'OPTIONS': {  # Options are not edited
        'driver': 'SQL Server',  # What it displays as on odbc admin   
        'dsn': 'System DSN',  # What it displays as on odbc admin
        'use_legacy_datetime': False
    }
like image 925
Will.Beasley Avatar asked Sep 24 '15 21:09

Will.Beasley


People also ask

Which SQL is best for Django?

PostgreSQL notes. Django supports PostgreSQL 11 and higher. psycopg2 2.8. 4 or higher is required, though the latest release is recommended.

Can I use Python with SQL Server?

However, Python when used along with SQL Servers, allows developers to perform real-time data streaming and operations in a much easier way. The Python SQL Server integration setup provides faster data processing as compared to using a network-based system over TCP, HTTP, etc.

Is Django good for database?

Django is especially useful for database-driven websites because of its Model-View-Template (MVT), and there's hardly any application without a database. The MVT allows developers to change the visual part of an app without affecting the business part of the logic, and vice versa.


3 Answers

Old question, but it might help someone. These are the settings I use in Windows to connect to a SQL Server. As @flipperpa said, I also use django-pyodbc-azure

pyodbc==3.0.10
django-pyodbc-azure==1.10.0.1

However, the pyodbc that pip will download doesn't work for me. As suggested in this answer, go this site http://www.lfd.uci.edu/~gohlke/pythonlibs/#pyodbc and download either depending on the version of Python 3.5 you have installed:

  • pyodbc‑3.0.10‑cp35‑none‑win32.whl if you have a 32-bit Python 3.5 install
  • pyodbc‑3.0.10‑cp35‑none‑win_amd64.whl if you have a 64-bit Python 3.5 install

Then use these settings to connect:

'default': {
 'ENGINE': 'sql_server.pyodbc',
 'HOST': "localhost",
 'USER': "--user name--",
 'PASSWORD': "--password--",
 'NAME': "--database name--",
 'PORT': 1433,
 'OPTIONS': {
      'driver' : 'SQL Server Native Client 11.0',
      'MARS_Connection' : True,
      'driver_supports_utf8' : True,
 },
}

For anyone that is using linux, MS has now released official odbc drivers for SQL server and is officially (if more than a little tacitly) supporting the django-pyodbc-azure project. I highly recommend using it and also the native linux SQL Server ODBC driver over FreeTDS.

like image 74
Diego Jancic Avatar answered Nov 09 '22 04:11

Diego Jancic


I've had my best luck with the following stack, and we're a Python 3 shop exclusively:

  • FreeTDS 0.95
  • pyodbc 3.0.10
  • django-pyodbc-azure 1.8.3 (assuming Django 1.8+)

Assuming you have odbc.ini, odbcinst.ini, and freetds.conf all squared away, here's an example of settings that work for me. Some of these depend on whether or not you're using SQL Server 2008+:

DATABASES = {
    'default': {
        'ENGINE': 'sql_server.pyodbc',
        'HOST': 'yourserver.com',
        'PORT': '1433',
        'NAME': 'your_db',
        'USER': 'your_user',
        'PASSWORD': 'your_pw',
        'AUTOCOMMIT': True,
        'OPTIONS': {
            'driver': 'FreeTDS',
            'unicode_results': True,
            'host_is_server': True,
            'extra_params': 'tds_version=7.2;',
        },
    },
}

You'll also need to include 'use_legacy_datetime': True, if you're running SQL Server 2005 or less, otherwise, it will use the new SQL Server 2008+ date fields. It will also automatically set to true if you're using an outdated driver. Good luck!

like image 24
FlipperPA Avatar answered Nov 09 '22 04:11

FlipperPA


Official backend fork from Microsoft: Microsoft MSSQL Django

They also have this reference Azure SQL Database Django sample.

Set 'ENGINE': 'mssql' to use it. Example:

DATABASES = {
    "default": {
        "ENGINE": "mssql",
        "NAME": "mydb",
        "USER": "user@myserver",
        "PASSWORD": "password",
        "HOST": "myserver.database.windows.net",
        "PORT": "",
        "OPTIONS": {
            "driver": "ODBC Driver 17 for SQL Server",
        },
    },
}
like image 1
Evandro Pomatti Avatar answered Nov 09 '22 04:11

Evandro Pomatti