← 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.