Understanding Django Migrations Concept and Workflow
📖 Scenario: You are building a simple Django app to manage a library's book collection. You need to create a model for books and understand how Django migrations help keep your database structure in sync with your models.
🎯 Goal: Learn how to create a Django model, generate migrations, and apply them to update the database schema step-by-step.
📋 What You'll Learn
Create a Django model named Book with fields title (CharField) and author (CharField).
Create a variable to hold the migration name.
Generate a migration file using the migration name variable.
Apply the migration to update the database.
💡 Why This Matters
🌍 Real World
Django migrations help developers keep the database structure in sync with their code models, making it easier to manage changes over time.
💼 Career
Understanding migrations is essential for backend developers working with Django to maintain and deploy database changes safely.
Progress0 / 4 steps
1
Create the Book model
In your Django app's models.py, create a model class called Book with two fields: title as a CharField with max length 100, and author as a CharField with max length 50.
Django
Hint
Use class Book(models.Model): to define the model. Use models.CharField(max_length=...) for the fields.
2
Create a migration name variable
Create a variable called migration_name and set it to the string 'create_book_model' to name your migration.
Django
Hint
Just assign the string 'create_book_model' to the variable migration_name.
3
Generate the migration file
Use the migration_name variable to generate a migration by writing the command python manage.py makemigrations --name create_book_model as a comment in your code to show the command you will run.
Django
Hint
Write the exact command as a comment, replacing the migration name with the variable's value.
4
Apply the migration to update the database
Write the command python manage.py migrate as a comment to apply the migration and update the database schema.
Django
Hint
Write the exact command python manage.py migrate as a comment.
Practice
(1/5)
1. What is the main purpose of Django migrations?
easy
A. To keep the database schema synchronized with Django models
B. To create HTML templates automatically
C. To manage user authentication
D. To optimize Django views for speed
Solution
Step 1: Understand what migrations do
Django migrations track changes in models and update the database schema accordingly.
Step 2: Compare options with this purpose
Only To keep the database schema synchronized with Django models describes keeping the database schema in sync with models, which is the core purpose of migrations.
Final Answer:
To keep the database schema synchronized with Django models -> Option A
Quick Check:
Migrations purpose = sync DB schema [OK]
Hint: Migrations sync models and database structure [OK]
Common Mistakes:
Confusing migrations with template or view management
Thinking migrations handle user authentication
Assuming migrations optimize performance
2. Which command do you run to create new migration files after changing models?
easy
A. python manage.py migrate
B. python manage.py makemigrations
C. python manage.py runserver
D. python manage.py collectstatic
Solution
Step 1: Identify the command for creating migration files
The command makemigrations generates migration files based on model changes.
Step 2: Differentiate from other commands
migrate applies migrations, runserver starts the server, and collectstatic collects static files.
Final Answer:
python manage.py makemigrations -> Option B
Quick Check:
Create migrations = makemigrations [OK]
Hint: Use makemigrations to create migration files [OK]
Common Mistakes:
Using migrate instead of makemigrations to create files
Hint: Always run makemigrations before migrate [OK]
Common Mistakes:
Thinking migrate creates migration files
Assuming server must run before migrate
Believing database must be deleted before migrating
5. You added a new field to a model and want to share the changes with your team. What is the correct workflow to update the database and share the changes safely?
hard
A. Delete the database, then run makemigrations and migrate on each machine
B. Run migrate first, then create migration files manually and share them
C. Only update the models.py file and ask team members to restart their servers
D. Run makemigrations, commit migration files to version control, then run migrate on all machines
Solution
Step 1: Create migration files after model changes
Run makemigrations to generate migration files reflecting the new field.
Step 2: Share migration files and apply them
Commit migration files to version control so the team can get them, then run migrate on each machine to update the database schema safely.
Final Answer:
Run makemigrations, commit migration files to version control, then run migrate on all machines -> Option D
Quick Check:
makemigrations + commit + migrate = safe team update [OK]
Hint: Commit migration files before migrating on other machines [OK]
Common Mistakes:
Running migrate before creating migration files
Deleting databases unnecessarily
Only updating models.py without migrations
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.