DED9

Database Models in Python and Django

In Django, database models are Python classes that define the structure of database tables. They use the ORM (Object-Relational Mapping) to map class attributes to table columns.
They simplify CRUD operations and relationships (e.g., ForeignKey, ManyToMany) by abstracting SQL, enabling intuitive database interactions.

Introduction

Django is a high-level Python web framework that simplifies building robust, scalable applications. A core feature is its Object-Relational Mapping (ORM) system.
It allows developers to define database models as Python classes and interact with databases using Python code instead of raw SQL. This makes database operations intuitive and secure.

This guide provides a comprehensive overview of database models in Django, covering model creation, relationships, migrations, and querying.
You’ll learn to design and manage databases effectively through practical examples (e.g., a blog application). By the end, you’ll be equipped to build data-driven applications with Django’s ORM.

1. Understanding Django’s ORMDatabase Models

What is a Database Model?

A database model in Django is a Python class representing a database table. Each class instance corresponds to a row in the table, and class attributes define table columns (fields).

Analogy: Think of a model as a blueprint for a table. The blueprint specifies column names and types (e.g., text, integer); each object created from it is a record in the table.

What is the ORM?

Django’s ORM maps Python classes to database tables, abstracting SQL operations. You write Python code to create, read, update, and delete (CRUD) data, and Django translates it into SQL for databases like PostgreSQL, MySQL, or SQLite.

Benefits:

2. Setting Up Django

To follow the examples, set up a Django project:

  1. Install Django: pip install django
  2. Create a project: django-admin startproject blog_project
  3. Create an app: cd blog_project && python manage.py startapp blog
  4. Add the app to INSTALLED_APPS in blog_project/settings.py:
    INSTALLED_APPS = [
        ...
        'blog.apps.BlogConfig',
    ]
    
    Configure the database in blog_project/settings.py (SQLite is default):
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': BASE_DIR / 'db.sqlite3',
        }
    }

3. Creating Database Models

Models are defined in an app’s models.py file. Each model inherits from django.db.models.Model.

Example: Blog Application Models

Let’s create models for a blog with posts and categories.

# blog/models.py from django.db import models class Category(models.Model): name = models.CharField(max_length=100, unique=True) description = models.TextField(blank=True) def __str__(self): return self.name class Meta: verbose_name_plural = "categories" class Post(models.Model): title = models.CharField(max_length=200) content = models.TextField() created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name='posts') def __str__(self): return self.title

Explanation:

4. Model Relationships

Django supports three main types of relationships:

Example: Adding Many-to-Many for Tags

# blog/models.py (updated) class Tag(models.Model): name = models.CharField(max_length=50, unique=True) def __str__(self): return self.name class Post(models.Model): title = models.CharField(max_length=200) content = models.TextField() created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name='posts') tags = models.ManyToManyField(Tag, related_name='posts', blank=True) def __str__(self): return self.title

Explanation:

5. Migrations

Django uses migrations to apply model changes to the database schema.

Steps

  1. Create Migrations: After defining models, generate migration files.
    python manage.py makemigrations
    

    This creates files  blog/migrations/ describing schema changes.

  2. Apply Migrations: Update the database.
    python manage.py migrate
    

    This creates tables (e.g., blog_category, blog_post, blog_tag).

Example Output:

Operations to perform: Apply all migrations: admin, auth, blog, contenttypes, sessions Running migrations: Applying blog.0001_initial... OK

Note: If you modify models (e.g., add a field), rerun makemigrations and migrate.

6. Querying the Database

Django’s ORM provides a powerful query API for CRUD operations.

Example: CRUD Operations

Set up a Django shell to test queries:

python manage.py shell

# Import models
from blog.models import Category, Post, Tag
from django.utils import timezone

# Create data
category = Category.objects.create(name="Tech", description="Technology topics")
post = Post.objects.create(
    title="Django Basics",
    content="Learn Django ORM",
    category=category
)
tag = Tag.objects.create(name="Python")
post.tags.add(tag)

# Read data
all_posts = Post.objects.all()  # Get all posts
tech_posts = Post.objects.filter(category__name="Tech")  # Filter by category
post_detail = Post.objects.get(title="Django Basics")  # Get single post

# Update data
post.content = "Updated content"
post.save()

# Delete data
post.delete()

# Print results
print(f"All Posts: {list(all_posts)}")
print(f"Tech Posts: {list(tech_posts)}")
print(f"Post Tags: {list(post_detail.tags.all())}")

Explanation:

Sample Output:

All Posts: [<Post: Django Basics>] Tech Posts: [<Post: Django Basics>] Post Tags: [<Tag: Python>]

Advanced Queries

7. Admin Interface Integration

Django’s admin interface automatically generates a UI for managing models.

Steps

  1. Register models in blog/admin.py:
    # blog/admin.py from django.contrib import admin from .models import Category, Post, Tag admin.site.register(Category) admin.site.register(Post) admin.site.register(Tag)
  2. Create a superuser:
    python manage.py createsuperuser
  3. Run the server:
    python manage.py runserver
  4. Access http://127.0.0.1:8000/admin/ and log in to manage data.

Customization (Optional):

# blog/admin.py @admin.register(Post) class PostAdmin(admin.ModelAdmin): list_display = ('title', 'category', 'created_at') list_filter = ('category', 'tags') search_fields = ('title', 'content')

Explanation:

8. Best Practices

9. Modern Trends (2025)

10. Next Steps

11. Conclusion

Django’s ORM and database models simplify building data-driven applications by abstracting SQL into Python classes. With models, you define tables, relationships, and constraints, while the ORM handles querying and migrations.
The blog example demonstrates creating models, managing relationships, and performing CRUD operations, providing a foundation for real-world projects.
Start with the provided code, experiment with your models, and leverage Django’s ecosystem to build robust applications.

Exit mobile version