~/TechPurAI
~/tutorials/django-from-scratch/models-and-the-orm
beginner·part 5 of 22·2 min read

Models and the ORM: defining your data

Updated Aug 15, 2026Python · Django

A Django model is a Python class that describes a database table — one class attribute per column, with the type of each attribute telling Django (and the eventual database) what kind of data belongs there. This part defines Post, the model the rest of the series builds around.

The Post model

python
# blog/models.py
from django.db import models
from django.conf import settings

class Post(models.Model):
    title = models.CharField(max_length=200)
    slug = models.SlugField(max_length=200, unique=True)
    content = models.TextField()
    author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    published = models.BooleanField(default=False)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        ordering = ["-created_at"]

    def __str__(self):
        return self.title

Every model inherits from models.Model — that's what gives it a database table, a manager (Post.objects), and everything else the ORM provides. CharField needs max_length because it maps to a fixed-width SQL column; TextField doesn't, since it's meant for unbounded content like a post body.

Field types worth understanding now

Meta and __str__

class Meta: ordering = ["-created_at"] sets the default order every query returns posts in — newest first, without needing .order_by() on every call site. __str__ isn't optional in any meaningful sense: without it, every Post shows up as Post object (1) in the admin (part 7) and the shell, instead of its actual title.

Common mistake

Defining a model and expecting a database table to exist immediately. It doesn't — a model is a Python description of a table, and the table itself only gets created once makemigrations generates a migration and migrate applies it, which is exactly what part 6 covers next.

VK

Vijay Kumar

Founder of TechPurAI — writing hands-on tutorials and honest tool breakdowns.

LinkedIn ↗
← previous4. Rendering templates: connecting a view to HTMLnext →6. Migrations: turning models into database tables