Issue
I have a model with several bool fields. I'd like to replace them with a single int field where each bit represents one of the old bools. I've added the new int field and run makemigrations
. Is there a way I can modify the migrations file and define how I'd like the new int fields' values to be defined? Like I said, I would like them to take the values of the previous bool fields.
Solution
Data migration is what you need.
- Add new field to the model, apply
makemigrations
. - Create a data migration, which would transfer data from old fields to the new one and run it.
- Remove old fields.
Data migration could be created with:
python manage.py makemigrations --empty yourappname
And inside it you should use RunPython
or RunSQL
depending on what exactly do you need.
Answered By - Yevgeniy Kosmak
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.