Home > Blockchain >  Create new Django model int field from existing bool fields
Create new Django model int field from existing bool fields

Time:01-30

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.

CodePudding user response:

Data migration is what you need.

  1. Add new field to the model, apply makemigrations.
  2. Create a data migration, which would transfer data from old fields to the new one and run it.
  3. 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.

  •  Tags:  
  • Related