We can connect Django to MSSQL(SQL Server) with Windows Authentication using mssql-django. *I use SQL Server 2019 Express.
First, set the code below to "settings.py" to connect Django to MSSQL with Windows Authentication. This code below is the example of Django and MSSQL in the same Windows Computer(localhost) and "ENGINE" must be "mssql" and "NAME" is for Database name "test" and "DESKTOP-QVRCPTA" for "HOST" is Windows Computer Name(Device name). *Keep it blank for "PORT" because there will be error if setting any port number e.g. "2244", "9877" or even "1433" which is the default port number of MSSQL:
# "settings.py"
DATABASES = {
'default':{
'ENGINE':'mssql', # Must be "mssql"
'NAME':'test', # DB name "test"
'HOST':'DESKTOP-QVRCPTA\SQLEXPRESS', # <server>\<instance>
'PORT':'',
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server',
},
}
}
In addition, "DESKTOP-QVRCPTA" can be replaced with "localhost" and "USER" and "PASSWORD" can be put with empty string and "PORT" can be removed as shown below to connect Django to MSSQL with Windows Authentication:
# "settings.py"
DATABASES = {
'default':{
'ENGINE':'mssql',
'NAME':'test',
'USER':'', # Here
'PASSWORD':'', # Here
'HOST':'localhost\SQLEXPRESS', # Here
# 'PORT':'', # Here
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server',
},
}
}
Next, install the latest package mssql-django:
pip install mssql-django
Then, make migrations and migrate:
python manage.py makemigrations && python manage.py migrate
Then, create superuser:
python manage.py createsuperuser
Now, we could connect Django to MSSQL with Windows Authentication, then create the tables for "test" database. *the default schema "dbo" is put before the table names as shown below:
And, this is Django Admin: