| title | Quickstart: Connect Django to SQL Server |
|---|---|
| description | Connect a Django application to SQL Server using mssql-django and run database operations with the Django ORM. |
| author | dlevy-msft-sql |
| ms.author | dlevy |
| ms.reviewer | randolphwest |
| ms.date | 06/22/2026 |
| ms.service | sql |
| ms.subservice | connectivity |
| ms.topic | quickstart |
| ai-usage | ai-assisted |
In this quickstart, you create a Django project, connect it to a SQL Server database using mssql-django, run migrations, and perform basic data operations with the Django ORM.
- Python 3.8 or later. Django 6.0 requires Python 3.12 and later versions.
- Microsoft ODBC Driver 17 or 18 for SQL Server. See Download ODBC Driver for SQL Server.
- A SQL Server instance or Azure SQL Database with a valid login.
Create a virtual environment and install the package:
python -m venv .venv.venv\Scripts\activate
pip install mssql-djangosource .venv/bin/activate
pip install mssql-djangoCreate a new Django project and app:
django-admin startproject myproject
cd myproject
python manage.py startapp myappEdit myproject/settings.py and replace the default DATABASES setting.
DATABASES = {
"default": {
"ENGINE": "mssql",
"NAME": "<your-database>",
"USER": "<your-username>",
"PASSWORD": "<your-password>",
"HOST": "<your-server>",
"PORT": "1433",
},
}[!INCLUDE trust-server-certificate-caution]
DATABASES = {
"default": {
"ENGINE": "mssql",
"NAME": "<your-database>",
"USER": "<your-username>",
"PASSWORD": "<your-password>",
"HOST": "<your-server>.database.windows.net",
"PORT": "1433",
"OPTIONS": {
"driver": "ODBC Driver 18 for SQL Server",
"extra_params": "Encrypt=yes",
},
},
}Edit myapp/models.py:
from django.db import models
class Product(models.Model):
name = models.CharField(max_length=100)
price = models.DecimalField(max_digits=10, decimal_places=2)
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.nameAdd "myapp" to INSTALLED_APPS in settings.py:
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"myapp",
]Generate and apply database migrations:
python manage.py makemigrations myapp
python manage.py migrateConfirm that the myapp migration was applied:
python manage.py showmigrations myappYou should see [X] 0001_initial. If you see [ ] 0001_initial, rerun python manage.py migrate myapp before continuing.
Open the Django shell. The shell is an interactive Python session with your Django project loaded, indicated by the >>> prompt.
python manage.py shellAt the >>> prompt, import the model:
from myapp.models import ProductCreate a record:
product = Product.objects.create(name="Widget", price=9.99)
print(f"Created: {product.name} (id={product.pk})")Read records:
for p in Product.objects.all():
print(f"{p.name}: ${p.price}")Update the record:
product.price = 12.99
product.save()Delete the record:
product.delete()Exit the shell with exit(). Alternatively, use Ctrl+Z on Windows, or Ctrl+D on Linux or macOS.
Note
If you get an Invalid object name 'myapp_product' error, the myapp_product table doesn't exist in the database even though Django's migration history claims 0001_initial is applied. Exit the shell, then reset the migration history and reapply it:
python manage.py migrate myapp zero --fake
python manage.py migrate myapp- Install mssql-django
- Django field to SQL Server type mappings
- Django tutorial
- mssql-django on GitHub
[!div class="nextstepaction"] mssql-django configuration reference