← Back to Django Course | Chapter 4: Models & Django ORM Basics | Lesson 1 of 10

Introduction to Django Models

A model is like a form template that tells Django exactly what boxes (fields) to draw in the database table for one kind of thing, like a Book or a Student.

What a Model Is

A Django model is a Python class that inherits from models.Model. Each attribute you define on it becomes a column in the database table Django creates for it.

Note: Model class names are singular and CamelCase, e.g. Book, not Books or book.

Example: What a Model Is

A Django model is a Python class that inherits from models.Model. Each attribute you define on it becomes a column in the database table Django creates for it.

markup
from django.db import models


class Book(models.Model):
    title = models.CharField(max_length=200)
    pages = models.IntegerField()

    def __str__(self):
        return self.title
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Where Models Live

Every Django app has its own models.py file. Django reads every model in every INSTALLED_APPS app to figure out what tables the whole project needs.

Warning: The app that defines the model must be listed in INSTALLED_APPS or Django won't detect it.

Example: Where Models Live

Every Django app has its own models.py file. Django reads every model in every INSTALLED_APPS app to figure out what tables the whole project needs.

markup
# myapp/models.py
from django.db import models


class Student(models.Model):
    name = models.CharField(max_length=100)
    grade = models.IntegerField()
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

From Model to Table

Defining a model class doesn't create the database table by itself. Django needs a migration to translate the Python class into real SQL that builds the table.

Note: Always run makemigrations then migrate right after adding or changing a model.

Example: From Model to Table

Defining a model class doesn't create the database table by itself. Django needs a migration to translate the Python class into real SQL that builds the table.

bash
python manage.py makemigrations
python manage.py migrate

⚠️ Run this command in your terminal.

A Complete Minimal Model

A working model needs at minimum a class name, an inherited base, and at least one field. Django automatically adds an id auto-incrementing primary key for you.

Example: A Complete Minimal Model

A working model needs at minimum a class name, an inherited base, and at least one field. Django automatically adds an id auto-incrementing primary key for you.

markup
from django.db import models


class Task(models.Model):
    title = models.CharField(max_length=150)
    is_done = models.BooleanField(default=False)

    def __str__(self):
        return self.title
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Common Mistakes
  1. Forgetting to run makemigrations and migrate after creating a model, so the database table never actually gets created.
  2. Defining a model but never registering it anywhere, then wondering why Django admin or queries don't see it.
  3. Naming the model class in lowercase (book) instead of CamelCase (Book), breaking Django's naming conventions.
Chapter Summary
  • A Django model is a Python class that inherits from models.Model and maps to one database table.
  • Each class attribute defined as a field becomes a column in that table.
  • Django auto-generates the SQL needed to create, read, update, and delete rows through the ORM.
  • Models live in an app's models.py file.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.