A Django web application can work with multiple databases such as SQLite, PostgreSQL, MySQL, Oracle, MS SQL Server, or even MongoDB (with third-party support).
Django uses a component called Database Backend (DB engine) configured in settings.py to communicate with different database systems.
ORM (Object Relational Mapping)
In Django, ORM stands for Object-Relational Mapping. It’s a powerful feature that allows developers to interact with the database using Python code instead of SQL.
ORM is a layer that translates Python objects (like classes and attributes) to database tables and columns, and vice versa.
Instead of writing raw SQL queries, you define models in Python, and Django ORM handles all the SQL behind the scenes.
How Django ORM Works
Define Models (Python Classes)
- You create models in
models.pyusing Django’sModelclass. - Each model represents a table, and each attribute becomes a column.
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=100)
author = models.CharField(max_length=100)
Migrations (Translate Models to SQL)
- Django generates SQL code to create the actual database schema.
python manage.py makemigrations
python manage.py migrate
Querying with Python
- Instead of writing SQL, you use model methods to perform CRUD operations.
# Create
Book.objects.create(title="Geetha", author="Vyasadeva")
# Read
books = Book.objects.all()
# Update
book = Book.objects.get(id=1)
book.title = "Bhagavad Gita"
book.save()
# Delete
book.delete()
Database Abstraction
- You can switch between databases (e.g., SQLite, PostgreSQL, MySQL) without changing your model code.
- Just update the
DATABASESsetting insettings.py.
When you call:
Book.objects.filter(author="Vyasa")
Django internally translates it to:
SELECT * FROM book WHERE author = 'Vyasa';
Handles all execution and result conversion automatically.
Django uses ORM, so your model code is the same regardless of the backend:
class Book(models.Model):
title = models.CharField(max_length=100)
author = models.CharField(max_length=100)

You don’t change the model when switching databases.
Architecture

Switching Databases
To switch databases:
- Install required adapter.
- Change
ENGINEinsettings.py. - Update credentials.
- Run
migrate.
No need to change your models or views.
Step-by-Step Process
Step 1: Install Django and the Required Database Driver
For SQLite – Built-in with Python. No installation needed.
For PostgreSQL – Install psycopg2:
pip install psycopg2
For MySQL – Install mysqlclient:
pip install mysqlclient
For MS SQL Server – Use third-party backend like django-mssql-backend:
pip install django-mssql-backend
For Oracle – Install cx_Oracle:
pip install cx_Oracle
For MongoDB – Use third-party ORM like djongo or mongoengine:
pip install djongo
Step 2: Configure DATABASES in settings.py
SQLite
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / "db.sqlite3",
}
}
PostgreSQL:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'your_db',
'USER': 'your_user',
'PASSWORD': 'your_password',
'HOST': 'localhost',
'PORT': '5432',
}
}
MySQL:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'your_db',
'USER': 'your_user',
'PASSWORD': 'your_password',
'HOST': 'localhost',
'PORT': '3306',
}
}
MS SQL Server:
DATABASES = {
'default': {
'ENGINE': 'mssql',
'NAME': 'your_db',
'USER': 'your_user',
'PASSWORD': 'your_password',
'HOST': 'localhost',
'PORT': '', # Default is 1433
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server',
},
}
}
Oracle:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.oracle',
'NAME': 'your_db',
'USER': 'your_user',
'PASSWORD': 'your_password',
'HOST': 'localhost',
'PORT': '1521',
}
}
MongoDB (djongo):
DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'your_db',
}
}
Step 3: Run Migrations
python manage.py makemigrations
python manage.py migrate
Django uses ORM, so your model code is the same regardless of the backend
Benefits of Django ORM
- Write clean, readable Python code instead of SQL.
- Easier to maintain and refactor.
- Automatically handles relationships, joins, and validations.
- Database-independent, Easily adaptable to various databases
- Same code works with different databases.