Models & Migrations
A Django model is a Python class that defines the structure of your data — each model maps to a single database table and each attribute maps to a column — and migrations are the version-controlled files Django generates to create and update those tables as your models change.
Learn Models & Migrations in our free Django course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Django course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
In this lesson you'll define your first model, learn the most common field types and their options, and master the makemigrations and migrate workflow that keeps your database in step with your code.
A model lives in your app's models.py file. You subclass models.Model and declare each column as a class attribute set to a field instance. Here is a Post model with several common field types:
The __str__ method tells Django (and the admin) how to display a single object. Without it, objects print as something unhelpful like Post object (1) .
Every field accepts options that control how it behaves and what the database column allows. These are the ones you'll use constantly:
Writing a model in models.py does not change the database by itself. You take two steps: makemigrations writes a migration file describing the change, and migrate applies it to the database.
A generated migration file is plain Python. It records the operations Django will perform:
A CharField always needs a max_length . Fill in the blank with the number 200 so the field definition is valid.
❌ Your models have changes that are not yet reflected in a migration
You edited a model but forgot to capture the change.
✅ Fix: run python manage.py makemigrations then python manage.py migrate .
❌ CharFields must define a 'max_length' attribute
A CharField was declared without a length limit.
✅ Fix: add a length, e.g. models.CharField(max_length=100) .
❌ You are trying to add a non-nullable field without a default
Adding a required field to a table that already has rows leaves them with no value.
✅ Fix: give the field a default= value, or make it null=True .
Model a simple Product with a name, a price, and an in-stock flag, then print a readable summary using its __str__ method.
Lesson 7 complete — your data has a schema!
You can now define models with the core field types, tune their behaviour with options like null , blank , default , and choices , and keep your database in sync using makemigrations and migrate .
🚀 Up next: The ORM: Queries — learn how to create, read, filter, and update rows with Django's query API.
Practice quiz
What does a Django model map to in the database?
- A single table
- A single column
- A whole database
- A SQL query
Answer: A single table. Each model maps to one database table.
What base class does a model subclass?
- forms.Form
- django.db.models.Model
- models.Field
- object only
Answer: django.db.models.Model. Models subclass django.db.models.Model.
Which field type requires a max_length?
- TextField
- IntegerField
- CharField
- BooleanField
Answer: CharField. CharField always needs a max_length.
Which field stores a true/false flag?
- IntegerField
- BooleanField
- CharField
- DateTimeField
Answer: BooleanField. BooleanField stores a true/false value.
What does the __str__ method do on a model?
- Runs a query
- Saves the row
- Tells Django how to display a single object
- Creates the table
Answer: Tells Django how to display a single object. __str__ provides the readable representation of an instance.
What does makemigrations do?
- Applies changes to the database
- Deletes the database
- Starts the server
- Writes a migration file describing model changes
Answer: Writes a migration file describing model changes. makemigrations plans the change by writing a migration file.
What does migrate do?
- Applies migration files to the database
- Generates a migration file
- Edits models.py
- Prints SQL only
Answer: Applies migration files to the database. migrate runs the migrations against the database.
What does auto_now_add=True do on a DateTimeField?
- Updates the time on every save
- Sets the timestamp once when the row is created
- Allows NULL
- Makes the field unique
Answer: Sets the timestamp once when the row is created. auto_now_add sets the timestamp once at creation.
For an optional text field, which option does Django convention prefer?
- null=True
- unique=True
- default=None
- blank=True (store '' not NULL)
Answer: blank=True (store '' not NULL). Prefer blank=True so empties store as an empty string.
Should migration files be kept in version control?
- No
- Yes, they are the shared schema history
- Only locally
- Only on the server
Answer: Yes, they are the shared schema history. Migrations are version-controlled so everyone rebuilds the same schema.