0
0
Djangoframework~30 mins

Migrations concept and workflow in Django - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Write the exact command python manage.py migrate as a comment.