Django supports multiple database backends, including PostgreSQL, MySQL, SQLite, and Oracle. To configure the database, you need to modify the DATABASES
setting in your project's settings.py
file.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql', # or 'mysql', 'sqlite3', 'oracle'
'NAME': 'your_database_name',
'USER': 'your_database_user',
'PASSWORD': 'your_database_password',
'HOST': 'your_database_host', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': 'your_database_port', # Set to empty string for default.
}
}
For SQLite, which is the default setting for Django, the configuration looks like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
Django models define the structure of our database tables. Each model maps to a single database table. Here is an example:
from django.db import models
class Author(models.Model):
name = models.CharField(max_length=100)
birth_date = models.DateField()
class Book(models.Model):
title = models.CharField(max_length=200)
author = models.ForeignKey(Author, on_delete=models.CASCADE)
published_date = models.DateField()
price = models.DecimalField(max_digits=10, decimal_places=2)
After defining models, we should create and apply migrations to sync our models with the database schema. This can be achieved by:
python manage.py makemigrations
python manage.py migrate
CRUD is an acronym that stands for Create, Read, Update, and Delete. These are the four basic operations that can be performed on data in a database.
author = Author(name='George Orwell', birth_date='1903-06-25')
author.save()
# Reading all books
all_books = Book.objects.all()
# Reading a specific book by ID
specific_book = Book.objects.get(id=1)
# Filtering books by author's name
books_by_author = Book.objects.filter(author__name='George Orwell')
book = Book.objects.get(id=1)
book.price = 24.99
book.save()
book = Book.objects.get(id=1)
book.delete()
Django provides several ways to optimize database queries:
select_related()
for foreign key relationships to reduce the number of database queries.
books = Book.objects.select_related('author').all()
prefetch_related()
for many-to-many and reverse foreign key relationships.
authors = Author.objects.prefetch_related('book_set').all()
class Book(models.Model):
title = models.CharField(max_length=200, db_index=True)
# other fields...
select_related()
or prefetch_related()
.
authors = Author.objects.prefetch_related('book_set').all()
for author in authors:
books = author.book_set.all() # No additional queries are made
from django.db.models import F, Sum
total_price = Book.objects.aggregate(total=Sum(F('price') * F('quantity')))