← Django tutorial

Django

Models

A Django model is a Python class describing one database table, each class attribute becomes a column.

Example: a Post model

# blog/models.py
from django.db import models

class Post(models.Model):
    title = models.CharField(max_length=200)
    body = models.TextField()
    published_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title
python manage.py makemigrations
python manage.py migrate

After changing a model, makemigrations generates a file describing exactly what changed, and migrate actually applies it to the database, creating a blog_post table with title, body, and published_at columns. This two-step cycle (generate, then apply) is deliberate: the generated migration file is itself real, reviewable, version-controlled code, so your whole team's database structure stays in sync through git, not through someone remembering to run a manual SQL command.

Example
# blog/models.py
from django.db import models

class Post(models.Model):
    title = models.CharField(max_length=200)
    body = models.TextField()
    published_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title
Output
python manage.py makemigrations && python manage.py migrate
creates a `blog_post` table with title, body, and published_at columns.
Ready to go further?

These free tutorials are a preview, our Backend Development cohort covers it in depth, with a real project and a cohort alongside you. Closes Nov 30

Enroll in Backend Development

Cookie preferences

Choose what you're comfortable with. You can change this any time from the footer.

Don't miss out

Ready to build something real?

Create a free account to enroll in a track, and get notified the moment a cohort opens.

Create free account