We are intenting to rework one of our models from old start-end date values to use starting date and length. However, this does pose a challenge in that we want to give default values to our new fields.
In this case, is it possible to run migration where we create new field, give it a value based on models old start-end fields
from datetime import date as Date
from django.db import models
class Period(models.Model):
#These are our new fields to replace old fields
period_length=models.IntegerField(default=12)
starting_date=models.DateField(default=Date.today)
#Old fields. We want to calculate the period length based on these before we remove them
start_day = models.IntegerField(default=1)
start_month = models.IntegerField(default=1)
end_day = models.IntegerField(default=31)
end_month = models.IntegerField(default=12)
Starting months and ending months can be anywhere between 1 and 12, so we need to run bunch of calculations to get the correct length. Is there a way for us to run a function in migrations that, after adding the new fields, calculates their new values before calling for removal of the old fields?
I do know I can create basic add/remove fields with makemigrations, but I want to add the value calculations in between. Other option I have considered is to first run a migration to add the fields, then a custom command to calculate fields and then a second migration that deletes the old fields, but this feels like it has greater chance of breaking something.