Hi👋 I'm Sourabh and student ambassador form Bangalore studying Computer Science from BIT
Integrating Django with Azure SQL
Migrating your Django project to a cloud-based database like Azure SQL can significantly enhance its scalability, reliability, and performance. This guide will walk you through the process of integrating Django with Azure SQL.
Prerequisites
- Azure Subscription: Get Azure for Students to receive $100 in credits for 12 months.
- Django Project: Ensure you have an existing Django project set up.
- VS Code: Install Visual Studio Code for your code editing needs.
Required Packages
pip install django pyodbc mssql-django
ODBC Driver installation
To connect Django to Azure SQL, you'll need to install the ODBC driver for SQL Server. The following command installs the ODBC Driver 17 for SQL Server depending on your operating system:
For Ubuntu/Linux:
sudo su
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
curl https://packages.microsoft.com/config/ubuntu/20.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
exit
sudo apt-get update
sudo ACCEPT_EULA=Y apt-get install -y msodbcsql17
sudo apt-get install -y unixodbc-dev
For macOS:
brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release
brew update
brew install --no-sandbox msodbcsql17 mssql-tools
For Windows:
You can download and install the driver from Microsoft's website.
Once the driver is installed, your Django project should be able to connect to Azure SQL using the mssql-django
package.
Why Azure SQL Over SQLite3?
- Scalability: Handles large datasets and high traffic effortlessly.
- Reliability: Managed by Microsoft, offering high availability and disaster recovery.
- Security: Advanced features like encryption, threat detection, and compliance.
- Performance: Features like automatic tuning and intelligent query processing.
- Remote Access: Access your database from anywhere.
Step-by-Step Integration
1. Set Up Azure SQL Database
📝Note - deployment can also be done through Azure Portal
- Create a Resource Group:
az group create -l <location> -n <MyResourceGroup>
- Create a SQL Server:
az sql server create -n <server-name> -l <location> --admin-user <admin-user> --admin-password <admin-password> -g <resource-group>
- Create the Database:
az sql db create -g <resource-group> -s <server-name> -n my-db --service-objective GP_Gen5_2
- Allow Your IP in Firewall:
az sql server firewall-rule create --resource-group <resource-group> --server <server-name> --name AllowMyClientIP --start-ip-address <your_public_ip> --end-ip-address <your_public_ip>
2. Install Required Packages
pip install django pyodbc mssql-django
3. Configure Django Settings
In settings.py
, update the DATABASES
section:
DATABASES = {
'default': {
'ENGINE': 'mssql',
'NAME': 'my-db',
'USER': '<admin-user>',
'PASSWORD': '<admin-password>',
'HOST': '<server-name>.database.windows.net',
'PORT': '',
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server',
},
}
}
4. Make Migrations and Migrate
python manage.py makemigrations
python manage.py migrate
5. Run the Server
python manage.py runserver
And that's it! Your Django project is now integrated with Azure SQL🎉.
Additional Resources:
Happy coding! 🚀
Useful Links
- Creating REST API with Python, Django, and Azure SQL
- Create REST API in Python with Django and Azure SQL
- Deploy Python (Django/Flask) App with PostgreSQL on Azure
- Azure SQL Migration with Data Studio
- Azure SQL Django Sample on GitHub
Top comments (0)