Django Models and ORM

Python 12 min min read Updated: Mar 09, 2026 Intermediate
Django Models and ORM
Intermediate Topic 5 of 10

Django Models & ORM

Django Models represent the structure of data stored in a database. They define fields, relationships, and behaviors of the data used in a Django application. Django provides a powerful ORM (Object Relational Mapping) system that allows developers to interact with the database using Python code instead of writing raw SQL queries.

The Django ORM simplifies database operations such as creating, retrieving, updating, and deleting data.

What is a Django Model?

A Django model is a Python class that represents a database table. Each attribute of the model represents a field in the table.

Django automatically generates database tables based on the models defined in the application.

Example of a Django Model

python from django.db import models class Post(models.Model): title = models.CharField(max_length=200) content = models.TextField() created_at = models.DateTimeField(auto_now_add=True) def __str__(self): return self.title

In this example:

  • Post represents the database table.
  • title, content, and created_at represent columns.
  • __str__() returns a readable representation of the object.

Common Django Model Fields

Field Type Description
CharField Stores short text strings
TextField Stores large text content
IntegerField Stores integer values
FloatField Stores decimal numbers
DateField Stores date values
DateTimeField Stores date and time values
BooleanField Stores true or false values
EmailField Stores email addresses

Applying Database Migrations

After creating models, Django needs to generate database tables using migrations.

Step 1: Create migration files

bash python manage.py makemigrations

Step 2: Apply migrations to the database

bash python manage.py migrate

This will create database tables based on the models.

What is Django ORM?

The Django ORM allows developers to interact with the database using Python objects instead of SQL queries. This makes database operations easier, safer, and more readable.

With ORM, developers can perform CRUD operations using Python code.

Creating Records

python post = Post.objects.create( title="First Post", content="This is my first Django post" )

This creates a new record in the database.

Retrieving Data

python posts = Post.objects.all() for post in posts: print(post.title)

This retrieves all records from the Post table.

Filtering Data

python posts = Post.objects.filter(title="First Post")

This retrieves records matching specific conditions.

Updating Data

python post = Post.objects.get(id=1) post.title = "Updated Title" post.save()

This updates a record in the database.

Deleting Data

python post = Post.objects.get(id=1) post.delete()

This removes a record from the database.

Model Relationships

Django models support relationships between tables.

Foreign Key (One-to-Many)

python class Comment(models.Model): post = models.ForeignKey(Post, on_delete=models.CASCADE) text = models.TextField()

Many-to-Many Relationship

python class Tag(models.Model): name = models.CharField(max_length=50) class Post(models.Model): title = models.CharField(max_length=200) tags = models.ManyToManyField(Tag)

One-to-One Relationship

python class Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE)

Advantages of Django ORM

  • No need to write raw SQL queries
  • Improves code readability
  • Provides database abstraction
  • Prevents SQL injection vulnerabilities
  • Works with multiple database engines

Conclusion

Django Models define the structure of database tables, while the Django ORM provides an easy way to interact with those tables using Python code. Together, they simplify database management and improve development productivity.

Understanding Django Models and ORM is essential for building dynamic web applications that interact with databases efficiently.

In the next tutorial, we will explore Django Views & URL Routing and learn how Django processes web requests and sends responses to users.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators