Microsoft SQL Server Django Backend

1.7 · active · verified Thu Apr 16

mssql-django is the official Microsoft-supported Django database backend for Microsoft SQL Server, Azure SQL, and SQL Database in Microsoft Fabric. It provides robust database connectivity for the Django web framework. The library is actively maintained with frequent releases, currently at version 1.7, offering broad compatibility with recent Django and Python versions.

Common errors

Warnings

Install

Imports

Quickstart

To use `mssql-django`, configure your Django project's `settings.py` file. The `ENGINE` value must be set to 'mssql'. Ensure your database credentials, host, and an appropriate ODBC driver are specified in the `DATABASES` dictionary. For Azure SQL, additional `extra_params` in `OPTIONS` might be necessary for encryption and certificate trust.

import os

DATABASES = {
    'default': {
        'ENGINE': 'mssql',
        'NAME': 'your_database_name',
        'USER': os.environ.get('DB_USER', 'your_db_user'),
        'PASSWORD': os.environ.get('DB_PASSWORD', 'your_db_password'),
        'HOST': os.environ.get('DB_HOST', 'your_server.database.windows.net'),
        'PORT': os.environ.get('DB_PORT', ''), # Default port 1433 if empty
        'OPTIONS': {
            'driver': os.environ.get('ODBC_DRIVER', 'ODBC Driver 18 for SQL Server'),
            # 'isolation_level': 'READ UNCOMMITTED', # Example option
            # 'extra_params': 'Encrypt=yes;TrustServerCertificate=no;', # Example for Azure SQL
        },
    }
}

# Optional: Disable pyodbc's connection pooling
# DATABASE_CONNECTION_POOLING = False

view raw JSON →