Package: django-azure-sql-backend
django-azure-sql-backendis a modern fork ofdjango-pyodbc-azuremade to support AAD access token authentication. It is also a fork ofdjango-pyodbc, aDjangoMicrosoft SQL Server external
DB backend that uses ODBC by employing thepyodbclibrary. It supports
Microsoft SQL Server and Azure SQL Database.FeaturesSupports Django 2.1Supports Microsoft SQL Server 2008/2008R2, 2012, 2014, 2016, 2017 and
Azure SQL DatabaseAAD authentication through registered application access tokenPasses most of the tests of the Django test suiteCompatible withMicosoft ODBC Driver for SQL Server,SQL Server Native Client,
andFreeTDSODBC driversDependenciesDjango 2.1pyodbc 3.0 or newerInstallationInstall pyodbc and DjangoInstall django-azure-sql-backendpip install django-azure-sql-backendNow you can point theENGINEsetting in the settings file used by
your Django application or project to the'sql_server.pyodbc'module path'ENGINE': 'sql_server.pyodbc'ConfigurationStandard Django settingsThe following entries in a database-level settings dictionary
in DATABASES control the behavior of the backend:ENGINEString. It must be"sql_server.pyodbc".NAMEString. Database name. Required.HOSTString. SQL Server instance in"server\instance"(on-premise) or"server.database.windows.net"(Azure SQL Database) format.PORTString. Server instance port.
An empty string means the default port.USERString. Database user name in"user"(on-premise) or"user@server"(Azure SQL Database) format.
If not given then MS Integrated Security will be used.PASSWORDString. Database user password.AUTOCOMMITBoolean. Set this to False if you want to disable
Django’s transaction management and implement your own.and the following entries are also available in the TEST dictionary
for any given database-level settings dictionary:NAMEString. The name of database to use when running the test suite.
If the default value (None) is used, the test database will use
the name “test_” +NAME.COLLATIONString. The collation order to use when creating the test database.
If the default value (None) is used, the test database is assigned
the default collation of the instance of SQL Server.DEPENDENCIESString. The creation-order dependencies of the database.
See the official Django documentation for more details.MIRRORString. The alias of the database that this database should
mirror during testing. Default value isNone.
See the official Django documentation for more details.AAD-AUTHWhen provided,USERandPASSWORDare not used and AAD authentication using
application access token is used instead.References:https://github.com/AzureAD/microsoft-authentication-library-for-pythonhttps://github.com/AzureAD/azure-activedirectory-library-for-python/wiki/Connect-to-Azure-SQL-Databasehttps://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-v2-python-webapphttps://docs.microsoft.com/en-us/sql/connect/python/pyodbc/step-3-proof-of-concept-connecting-to-sql-using-pyodbc?view=sql-server-ver15Dictionary. Current available keys are:tenant_idString. Refers to the registered application tenant identifier to use.
It is also known as the directory identifier and can sometimes be provided
within the STS url like so:https://login.microsoftonline.com/<TENANT_ID>/oauth2/v2.0/tokenclient_idString. Refers to the registered application client identifier to use.
It is also known as the application identifier.secretString. Refers to the secret that will be use to authenticate with AAD.OPTIONSDictionary. Current available keys are:driverString. ODBC Driver to use ("ODBC Driver 13 for SQL Server","SQL Server Native Client 11.0","FreeTDS"etc).
Default is"ODBC Driver 13 for SQL Server".isolation_levelString. Setstransaction isolation levelfor each database session. Valid values for this entry areREAD UNCOMMITTED,READ COMMITTED,REPEATABLE READ,SNAPSHOT, andSERIALIZABLE. Default isNonewhich means
no isolation levei is set to a database session and SQL Server default
will be used.dsnString. A named DSN can be used instead ofHOST.host_is_serverBoolean. Only relevant if using the FreeTDS ODBC driver under
Unix/Linux.By default, when using the FreeTDS ODBC driver the value specified in
theHOSTsetting is used in aSERVERNAMEODBC connection
string component instead of being used in aSERVERcomponent;
this means that this value should be the name of adataserverdefinition present in thefreetds.confFreeTDS configuration file
instead of a hostname or an IP address.But if this option is present and it’s value isTrue, this
special behavior is turned off.Seehttp://www.freetds.org/userguide/dsnless.htmfor more information.unicode_resultsBoolean. If it is set toTrue, pyodbc’sunicode_resultsfeature
is activated and strings returned from pyodbc are always Unicode.
Default value isFalse.extra_paramsString. Additional parameters for the ODBC connection. The format is"param=value;param=value".collationString. Name of the collation to use when performing text field
lookups against the database. Default isNone; this means no
collation specifier is added to your lookup SQL (the default
collation of your database will be used). For Chinese language you
can set it to"Chinese_PRC_CI_AS".connection_timeoutInteger. Sets the timeout in seconds for the database connection process.
Default value is0which disables the timeout.connection_retriesInteger. Sets the times to retry the database connection process.
Default value is5.connection_retry_backoff_timeInteger. Sets the back off time in seconds for reries of
the database connection process. Default value is5.query_timeoutInteger. Sets the timeout in seconds for the database query.
Default value is0which disables the timeout.backend-specific settingsThe following project-level settings also control the behavior of the backend:DATABASE_CONNECTION_POOLINGBoolean. If it is set toFalse, pyodbc’s connection pooling feature
won’t be activated.ExampleHere is an example of the database settings using user and password:DATABASES = {
    'default': {
        'ENGINE': 'sql_server.pyodbc',
        'NAME': 'mydb',
        'USER': 'user@myserver',
        'PASSWORD': 'password',
        'HOST': 'myserver.database.windows.net',
        'PORT': '',

        'OPTIONS': {
            'driver': 'ODBC Driver 13 for SQL Server',
        },
    },
}

# set this to False if you want to turn off pyodbc's connection pooling
DATABASE_CONNECTION_POOLING = FalseHere is an example of the database settings using AAD access token authentication:DATABASES = {
    'default': {
        'ENGINE': 'sql_server.pyodbc',
        'NAME': 'mydb',
        'HOST': 'myserver.database.windows.net',
        'PORT': '',
        'AAD-AUTH': {
            'tenant_id': '02a2e49f-b581-45c4-84a9-bdee0198b26f',
            'client_id': '818979f8-a731-48d9-bf42-b00a04e1e618',
            'secret': "MY_SUPER_SECRET",
        },
        'OPTIONS': {
            'driver': 'ODBC Driver 13 for SQL Server',
        },
    },
}

# set this to False if you want to turn off pyodbc's connection pooling
DATABASE_CONNECTION_POOLING = FalseLimitationsThe following features are currently not supported:Altering a model field from or to AutoField at migrationNoticeThis version ofdjango-azure-sql-backendonly supports Django 2.1.
If you want to use it on older versions of Django,
specify an appropriate version number (2.0.x.x for Django 2.0)
at installation like this:pip install "django-azure-sql-backend<2.1"