0
0
Djangoframework~30 mins

Database migration in production in Django - Mini Project: Build & Apply

Choose your learning style9 modes available
Database migration in production
📖 Scenario: You are working on a Django web application that is live in production. You need to add a new field to an existing model and safely migrate the database without downtime or data loss.
🎯 Goal: Learn how to create a new field in a Django model, generate and apply migrations, and handle the migration process carefully in a production environment.
📋 What You'll Learn
Create a new field in an existing Django model
Generate a migration file using Django management commands
Apply the migration to the production database
Ensure the migration is safe and does not cause downtime
💡 Why This Matters
🌍 Real World
Adding new features or fixing bugs often requires changing the database schema in live Django applications without causing downtime or data loss.
💼 Career
Database migration skills are essential for backend developers and DevOps engineers working with Django in production environments.
Progress0 / 4 steps
1
Add a new field to the Django model
Open the models.py file in your Django app and add a new field called is_active of type models.BooleanField with default value True to the existing UserProfile model.
Django
Need a hint?

Use is_active = models.BooleanField(default=True) inside the UserProfile class.

2
Generate the migration file
Run the Django management command python manage.py makemigrations to create a migration file for the new is_active field.
Django
Need a hint?

Use the exact command python manage.py makemigrations in your terminal to generate migration files.

3
Apply the migration to the production database
Run the Django management command python manage.py migrate to apply the new migration and update the production database schema.
Django
Need a hint?

Use the exact command python manage.py migrate to apply migrations to the database.

4
Ensure safe migration practices
Add a comment in your code reminding to backup the database and test migrations in a staging environment before applying to production.
Django
Need a hint?

Add a clear comment reminding to backup and test migrations before production deployment.