Skip to content
Advertisement

Handling data during django migrations?

JavaScript

I have created this model. Let suppose I have added a new field called

JavaScript

After that If I run command python manage.py makemigrations, then django will add a new field status in table. Let’s I have 3 rows in the table, then value will be as below:

JavaScript

With this migration, I also want to change the status of every row and add new row also like

JavaScript

Is there any way to handle data manipulation along the schema migration in django?

Advertisement

Answer

You can set individual value by using Special Operations such as django.db.migrations.RunPython.

(ref: https://docs.djangoproject.com/en/3.1/ref/migration-operations/#runpython)

Suppose you have added a field ‘status’ to the model and have already completed making the migration(executing manage.py makemigrations).

Now, execute manage.py makemigrations your_app --empty. Then, edit the auto-created migration file like below.

JavaScript

And then, execute manage.py migrate.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement