django models and migrations
No account is required. Learn the material, try the examples, and mark it complete when you are ready to move on. Sign in with email only if you want to save progress across devices.
Tutorial 10 - Django Models and Migrations
Why this matters
Applications need to remember information.
A booking application remembers bookings. An inventory application remembers products. A CRM remembers customers.
Django models describe this data in Python.
1. A model
from django.db import models
class Product(models.Model):
name = models.CharField(max_length=100)
price = models.DecimalField(
max_digits=10,
decimal_places=2,
)This describes a product record.
Conceptually:
Product
├── name
└── price2. Common fields
You will commonly encounter:
models.CharField()
models.TextField()
models.IntegerField()
models.DecimalField()
models.BooleanField()
models.DateField()
models.DateTimeField()
models.EmailField()For example:
class Booking(models.Model):
name = models.CharField(max_length=100)
email = models.EmailField()
date = models.DateField()
confirmed = models.BooleanField(default=False)3. The database connection
A relational database stores records in tables.
Your model:
class Product(models.Model):
name = ...
price = ...corresponds conceptually to:
product table
id | name | price
---|----------|------
1 | Laptop | 50000
2 | Mouse | 1000Django's ORM lets you work with these records using Python.
4. What is a migration?
Changing models.py does not automatically change the database.
Suppose you add:
description = models.TextField()Your Python model now expects a new field.
The database also needs a structural change.
Migrations are Django's mechanism for recording and applying those changes.
5. Creating migrations
Run:
python manage.py makemigrationsDjango examines your models and creates a migration file.
You might see:
Migrations for 'main':
main/migrations/0002_add_description.py6. Applying migrations
Then:
python manage.py migrateThink:
makemigrations
↓
Create instructions
migrate
↓
Apply instructions7. Why you might see "no such table"
Suppose your code executes:
Product.objects.all()and Django reports:
django.db.utils.OperationalError:
no such table: main_productThe likely situation is:
Python model exists
↓
Database table does notCheck:
python manage.py showmigrationsThen:
python manage.py migrateIf you changed a model:
python manage.py makemigrations
python manage.py migrate8. Creating records
Product.objects.create(
name="Mouse",
price=1000,
)9. Reading records
All:
Product.objects.all()One:
Product.objects.get(id=1)Filtered:
Product.objects.filter(price__gt=1000)This is Django's ORM.
10. Updating
product = Product.objects.get(id=1)
product.price = 1200
product.save()The save() writes the change to the database.
11. Deleting
product.delete()Deletion should be treated carefully because data can be lost.
12. Migrations and Git
Migration files normally belong in Git:
main/migrations/
0001_initial.py
0002_add_description.pyThey describe database changes.
The local database itself should generally not be treated as source code.
13. Exercise
Create:
class Service(models.Model):
name = models.CharField(max_length=100)
price = models.DecimalField(
max_digits=10,
decimal_places=2,
)Then:
python manage.py makemigrations
python manage.py migrateRegister it in Admin.
Create a record.
Display it through a view and template.
Trace:
Model
↓
Migration
↓
Database
↓
Admin
↓
View
↓
Template14. Using ChatGPT
If migrations fail, provide the exact error, models.py, migration output, and showmigrations output.
Ask:
Explain why my Django model and database schema are out of sync and give me the smallest safe fix.
Do not delete migrations or databases just because an AI suggests it without explaining why.
Remember
models.py
↓
makemigrations
↓
migration files
↓
migrate
↓
database schemaModels describe data. Migrations keep database structure synchronized with those models.
Finished this lesson?
Keep your place here, or save it across devices with an email link.
Saved in this browser. Save progress with email →