Introduction to Django Models
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.
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.
# 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.
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.
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. #}
- Forgetting to run makemigrations and migrate after creating a model, so the database table never actually gets created.
- Defining a model but never registering it anywhere, then wondering why Django admin or queries don't see it.
- Naming the model class in lowercase (book) instead of CamelCase (Book), breaking Django's naming conventions.
- 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.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: