Migration for references that is run every time?

Say I have a model Doctor that has a Many to Many relationship to AppointmentType.
AppointmentType could be ‘Online’,‘Face to Face’,‘Telephone’

I would model this with a ‘through’ table e.g Doctor → DoctorByAppointmentType → AppointmentType.

In my migrations I would add some RunPython that would populate AppointmentType with the above three values.

If I wanted to add a new value say ‘Email’ I will then have to create a new migration file and write the logic to insert the new value.

My thoughts here are that it would be really useful to have a migrations file that was run idempotently every deployment that could take care of use cases like these(reference tables).

I’m also asking the question to see how other people may handle this kind of scenario and if creating a manual migration is the best way to handle this scenario?


That’s something that fixtures are good for.

Also see:

To have data always loadindg during migrate, without the need for another command like fixtures, you can use a post_migrate handler function. This is how Django populates the ContentType model. See the code: the function and its registration in AppConfig.ready. I blogged about this a while ago.

I’d use this only for populating a small amount of data, like a few hundred rows. For example, in one client project, I added such a handler function for loading the list of timezones from the system database. It sounds like this fits your use case.

Thank you both.

post_migrate sounds like exactly what I need :smiley: