codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐍

Python Programming

61 / 68 topics
59Web Development with Flask60Django Framework Basics61Django ORM & Models62Django Templates & Jinja263Building REST APIs with Django Rest Framework (DRF)
Tutorials/Python Programming/Django ORM & Models
🐍Python Programming

Django ORM & Models

Updated 2026-04-20
4 min read

Django ORM & Models

Introduction

Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. One of its core features is the Object-Relational Mapping (ORM) system, which abstracts database interactions and allows developers to work with data using Python code instead of SQL queries.

In this tutorial, we'll explore Django's ORM and models in detail, including how to define models, perform database operations, and best practices for working with them.

What is Django ORM?

Django ORM is a powerful tool that translates Python classes into database tables. It allows you to interact with the database using Python code, making it easier to manage data without writing raw SQL queries. The ORM provides an abstraction layer between your application's business logic and the underlying database, which simplifies development and enhances maintainability.

Setting Up Django

Before we dive into models and ORM, ensure you have Django installed. You can install Django using pip:

pip install django

Once installed, create a new Django project:

django-admin startproject myproject
cd myproject

Create a new app within your project:

python manage.py startapp myapp

Add the app to your INSTALLED_APPS in myproject/settings.py:

INSTALLED_APPS = [
    ...
    'myapp',
]

Defining Models

Models in Django are Python classes that define the structure of your database tables. Each model corresponds to a single database table, and each attribute of the class represents a field in the table.

Basic Model Definition

Here's an example of a simple Book model:

from django.db import models

class Book(models.Model):
    title = models.CharField(max_length=200)
    author = models.CharField(max_length=100)
    published_date = models.DateField()
    isbn = models.CharField(max_length=13, unique=True)
    page_count = models.IntegerField(null=True, blank=True)
    cover_url = models.URLField(null=True, blank=True)
    language = models.CharField(max_length=2)

    def __str__(self):
        return self.title

Explanation

  • models.Model: The base class for all Django models.
  • title, author, etc.: Fields that define the structure of the table. Each field is an instance of a model field class, such as CharField, DateField, or IntegerField.
  • __str__(): A method to return a string representation of the object, useful for debugging and in the Django admin interface.

Field Types

Django provides various field types to define different kinds of data:

  • CharField(max_length=...): Fixed-length character string.
  • TextField(): Variable-length character string.
  • IntegerField(), FloatField(): Numeric fields.
  • DateField(), DateTimeField(): Date and time fields.
  • BooleanField(): Boolean field (True/False).
  • ForeignKey(), ManyToManyField(): Relationship fields.

Meta Options

You can add metadata to your models using the Meta class:

class Book(models.Model):
    ...
    
    class Meta:
        ordering = ['title']
        verbose_name = 'Book'
        verbose_name_plural = 'Books'
  • ordering: Default ordering for the model.
  • verbose_name, verbose_name_plural: Human-readable names for the model.

Migrations

After defining your models, you need to create and apply migrations to update the database schema:

python manage.py makemigrations
python manage.py migrate

makemigrations creates migration files that describe changes to your models. migrate applies these changes to the database.

Performing Database Operations

Django ORM provides a rich API for interacting with the database. Here are some common operations:

Creating Objects

To create a new object, instantiate the model and call save():

from myapp.models import Book

book = Book(title="1984", author="George Orwell", published_date="1949-06-08", isbn="9780451524935")
book.save()

Retrieving Objects

You can retrieve objects using various methods:

# Get a single object by primary key
book = Book.objects.get(pk=1)

# Filter objects
books = Book.objects.filter(author="George Orwell")

# Order objects
books = Book.objects.order_by('title')

# Limit results
books = Book.objects.all()[:5]

Updating Objects

To update an object, modify its attributes and call save():

book = Book.objects.get(pk=1)
book.title = "Animal Farm"
book.save()

Deleting Objects

To delete an object, call the delete() method:

book = Book.objects.get(pk=1)
book.delete()

QuerySet API

Django's ORM provides a powerful QuerySet API for querying and manipulating data. Here are some advanced features:

Chaining Filters

You can chain multiple filters to refine your queries:

books = Book.objects.filter(author="George Orwell").order_by('title')

Aggregations

Django supports aggregations, which allow you to perform calculations on querysets:

from django.db.models import Avg

average_pages = Book.objects.aggregate(Avg('page_count'))

Annotations

Annotations add computed fields to each object in a queryset:

from django.db.models import Count

books_with_authors = Book.objects.annotate(num_authors=Count('author'))

Best Practices

  1. Use get_or_create(): When you need to retrieve or create an object, use get_or_create() to avoid race conditions.

    book, created = Book.objects.get_or_create(title="Brave New World", author="Aldous Huxley")
    
  2. Batch Updates: For large datasets, use update() to perform batch updates instead of iterating over objects and calling save().

    Book.objects.filter(author="George Orwell").update(page_count=328)
    
  3. Select Related: Use select_related() for single-valued relationships to reduce database queries.

    books = Book.objects.select_related('author').all()
    
  4. Prefetch Related: Use prefetch_related() for multi-valued relationships to optimize performance.

    books = Book.objects.prefetch_related('reviews').all()
    
  5. Use Managers: Define custom managers to encapsulate complex queries and make your code more readable.

    class PublishedBookManager(models.Manager):
        def get_queryset(self):
            return super().get_queryset().filter(published_date__isnull=False)
    
    class Book(models.Model):
        ...
        objects = models.Manager()
        published_objects = PublishedBookManager()
    
  6. Avoid Raw SQL: Use Django's ORM as much as possible to maintain abstraction and portability.

  7. Use annotate() and aggregate() Wisely: These methods can be powerful but can also lead to inefficient queries if not used carefully.

Conclusion

Django's ORM is a versatile and powerful tool that simplifies database interactions in Python applications. By understanding how to define models, perform CRUD operations, and optimize queries using the QuerySet API, you can build efficient and maintainable web applications with Django.

In this tutorial, we covered the basics of Django ORM, including model definitions, migrations, and common database operations. We also explored advanced features like aggregations and annotations, as well as best practices for working with models in a production environment.


PreviousDjango Framework BasicsNext Django Templates & Jinja2

Recommended Gear

Django Framework BasicsDjango Templates & Jinja2