Skip to main content

Django Backend for Microsoft SQL

mssql-django is the official Microsoft‑supported Django database backend for SQL Server, Azure SQL and SQL Database in Microsoft Fabric.

It provides a reliable, enterprise‑grade database connectivity option for the Django web framework, enabling Python developers to build and run production‑ready applications on Microsoft’s data platform.

This project is the continuation and evolution of earlier community efforts, and it builds on the strong foundation established by django-mssql-backend and its predecessors. mssql-django focuses on long‑term stability, performance, security, and compatibility with both Django and SQL Server.

Supportability

Component Supported Versions
Django 3.2, 4.0, 4.1, 4.2, 5.0, 5.1, 5.2, 6.0, 6.1
Python 3.8 – 3.14 (Django 6.0 and 6.1 require 3.12+)
SQL Server 2016, 2017, 2019, 2022, 2025
Azure SQL Database, Managed Instance, SQL Database in Microsoft Fabric
ODBC Driver Microsoft ODBC Driver 17 or 18 for SQL Server
FreeTDS Supported via FreeTDS ODBC driver

Quick Start

  1. Install mssql-django (pulls in Django, pyodbc, and pytz automatically):

    pip install mssql-django
    
  2. Configure your settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'mssql',
        'NAME': 'mydb',
        'USER': 'user@myserver',
        'PASSWORD': 'password',
        'HOST': 'myserver.database.windows.net',
        'PORT': '',
        'OPTIONS': {
            'driver': 'ODBC Driver 18 for SQL Server',
        },
    },
}

# set this to False if you want to turn off pyodbc's connection pooling
DATABASE_CONNECTION_POOLING = False

Configuration Reference

Standard Django Settings

Setting Type Description
ENGINE String Must be "mssql"
NAME String Database name. Required.
HOST String SQL Server instance in "server\instance" format
PORT String Server instance port. Empty string means default port.
USER String Database user name. If not given, MS Integrated Security is used.
PASSWORD String Database user password
TOKEN String Access token for Azure AD auth (e.g. via azure.identity)
AUTOCOMMIT Boolean Set to False to disable Django's transaction management
Trusted_Connection String Default "yes". Set to "no" if required.

TEST Settings

Setting Type Description
NAME String Test database name. Default: "test_" + NAME
COLLATION String Collation for test database. Default: instance default.
DEPENDENCIES String Creation-order dependencies of the database
MIRROR String Alias of database to mirror during testing

OPTIONS

Option Type Default Description
driver String "ODBC Driver 18 for SQL Server" ODBC driver to use. Auto-falls back to Driver 17 if 18 is not installed.
isolation_level String None Transaction isolation level: READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ, SNAPSHOT, or SERIALIZABLE
dsn String Named DSN, can be used instead of HOST
host_is_server Boolean False Set to True to use HOST/PORT directly with FreeTDS instead of a freetds.conf dataserver name. Details
unicode_results Boolean False Activate pyodbc's unicode_results feature
extra_params String Additional ODBC params ("param=value;param=value"). Use for Azure AD Authentication.
collation String None Collation for text field lookups (e.g. "Chinese_PRC_CI_AS")
connection_timeout Integer 0 Connection timeout in seconds (0 = disabled)
connection_retries Integer 5 Number of connection retry attempts
connection_retry_backoff_time Integer 5 Back-off time in seconds between retries
query_timeout Integer 0 Query timeout in seconds (0 = disabled)
setencoding / setdecoding List pyodbc encoding / decoding config
return_rows_bulk_insert Boolean False Allow returning rows from bulk insert. Must be False if tables have triggers.

Backend-Specific Settings

Setting Type Default Description
DATABASE_CONNECTION_POOLING Boolean True Set to False to disable pyodbc's connection pooling

Known Limitations

The following limitations apply when using SQL Server with Django:

  • Altering a model field from or to AutoField at migration
  • Floating point arithmetic in some annotate functions
  • Annotate/exists function in order_by
  • Righthand power and arithmetic with datetimes
  • Timezones and timedeltas not fully supported
  • Rename field/model with foreign key constraint
  • Database level constraints and filtered indexes
  • Date extract function
  • Bulk insert with triggers and returning rows

Version-Specific Notes

Version Notes
Django 5.1 Minor limitations with composite primary key inspection via inspectdb
Django 5.2 Tuple lookups require Django 5.2.4+ for full support. Some JSONField bulk/CASE WHEN update edge cases. See test exclusions for details.
Django 6.0 Requires Python 3.12+. All 5.2 limitations apply. Backend handles all 6.0 API changes transparently.
Django 6.1 Requires Python 3.12+. All 6.0 limitations apply. Two 6.1 additions are unavailable: database-level referential actions (DB_CASCADE, DB_SET_NULL, DB_SET_DEFAULT) are not supported because SQL Server disallows multiple cascade paths to the same table, so using one raises a Django system check (fields.E324) that points you to the standard Django-level on_delete; and bitwise aggregates (BitAnd, BitOr, BitXor) are not implemented by this backend and raise NotSupportedError. See test exclusions for details.

JSONField lookups have additional limitations — see the JSONField wiki page.

Helpful Links

Resource Link
Wiki & Guides mssql-django Wiki
Contributing Contributing Guide
Code of Conduct Microsoft Open Source Code of Conduct

Still have questions?

Check the FAQ or open an issue on GitHub.

Contributing

We welcome contributions and suggestions! See CONTRIBUTING.md for details.

All contributors are listed on GitHub: Contributor Insights

Most contributions require a Contributor License Agreement (CLA). For details, visit https://cla.opensource.microsoft.com. A CLA bot will guide you when you submit a pull request.

This project has adopted the Microsoft Open Source Code of Conduct.

Security

For security reporting instructions please refer to SECURITY.md.

Trademarks

This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft trademarks or logos is subject to and must follow Microsoft's Trademark & Brand Guidelines. Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party's policies.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mssql_django-1.8.0.tar.gz (74.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mssql_django-1.8.0-py3-none-any.whl (76.3 kB view details)

Uploaded Python 3

File details

Details for the file mssql_django-1.8.0.tar.gz.

File metadata

  • Download URL: mssql_django-1.8.0.tar.gz
  • Upload date:
  • Size: 74.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: RestSharp/106.13.0.0

File hashes

Hashes for mssql_django-1.8.0.tar.gz
Algorithm Hash digest
SHA256 023a3d55a85d1d0d9f38d731182c35c2fecbf6bdc7b2464ed7ebf29c9e7bbdb2
MD5 542d3ae2805fbd3160e1a28b42032df6
BLAKE2b-256 b1a848d00b41ee1c7057ec9169ba472725f2e7342eacfec667802b25d5ebf1fe

See more details on using hashes here.

File details

Details for the file mssql_django-1.8.0-py3-none-any.whl.

File metadata

  • Download URL: mssql_django-1.8.0-py3-none-any.whl
  • Upload date:
  • Size: 76.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: RestSharp/106.13.0.0

File hashes

Hashes for mssql_django-1.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1e97e303477ea228454e76514e544081d6041a5a4383f0c40a9313549d4724f2
MD5 e45b9e1673c826ddd65f05d2bc4bc066
BLAKE2b-256 090b37df91c84149e51c504c301d665de103274890da578d9831d8ca28a000c2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page