A Beginner's Guide to Django Models (With Real Examples)
Models are the heart of any Django project. A plain-English walkthrough of what they are, why they matter, and how to write your first one.
If you have heard that Django is "Python for websites" but not much else, this is for you. The single most important concept in Django is the model, so let's start there.
What a model actually is
A model is a Python class that describes one kind of thing in your database — a blog post, a product, a student. Each attribute of the class becomes a column in a database table.
from django.db import models class Article(models.Model): title = models.CharField(max_length=200) body = models.TextField() published = models.BooleanField(default=False) created_at = models.DateTimeField(auto_now_add=True)That class gives you Article.objects.create(title="Hello", body="…"), plus filtering, sorting, pagination and relationships — all without you writing a line of SQL.
Field types you will use constantly
- CharField — short text, always needs max_length
- TextField — long text, no limit
- IntegerField / DecimalField — numbers (use DecimalField for money)
- BooleanField — true/false, with default=
- DateTimeField / DateField — dates; auto_now_add sets it on create
- ForeignKey — a reference to another model, the thing that makes Django shine
Relationships are the real power
A blog post has a category and an author. A course has many enrolments. Once your models relate, the admin panel builds itself — filters, dropdowns, related lists, all of it. This is why we build client projects on Django: the client can run their site without a developer.
Slugify, always
Never use an auto-incrementing ID in a public URL. Generate a slug from the title so your URLs are readable and stay stable if the title changes:
def save(self, *args, **kwargs): if not self.slug: base = slugify(self.title) slug, n = base, 2 while Article.objects.filter(slug=slug).exclude(pk=self.pk).exists(): slug = f"{base}-{n}" n += 1 self.slug = slug super().save(*args, **kwargs)Learn models properly and the rest of Django stops feeling like magic and starts feeling like a very sensible library.
Where to go next
Add the model, run python manage.py makemigrations, then migrate, register it in admin.py, and you have a working content system. We teach exactly this in our Django course in Surkhet, alongside a real project to put it in your portfolio.