Intermediate
How do you create and apply migrations in Django?
Migrations in Django are used to propagate changes you make to your models (adding a field, deleting a model, etc.) into the database schema. To create and apply migrations, follow these steps:
- Make changes to your models in
models.py
. - Run
to create a migration file.python manage.py makemigrations
- Run
to apply the migration to the database.python manage.py migrate
This process ensures your database schema is synchronized with your models. For instance, if you add a new field published_date
to a model, running these commands updates the database accordingly.