Ad

Django: How To Prompt User During Migration?

- 1 answer

I'm writing a migration that converts a ForeignKeyField to a ManyToManyField. Part of this migration involves data migration. Migrating forwards is easy and requires no user interaction. Migrating backwards however ideally requires the user to pick which of the m2m to become the foreign key (ie there's a recognition of data loss).

Additionally. the user performing the migration might want to be given a python shell so they can poke around and look at objects before answering the question.

And some users might not care, and might not want to be prompted at all. (ie they use the --no-input argument during migration)

Does django have a built in mechanism to do all this? I can't find anything, but at the back of my mind it seems like something I've seen somewhere.

Ad

Answer

If you really need to interactively ask for user input during a migration, you can use the InteractiveMigrationQuestioner in django.db.migrations.questioner.

Subclass it, add your own question (e.g. ask_which_instance) with its arguments. You can use the _ask_choice method to let users choose from multiple options.

Then in your backward() method, instantiate it and call your question method (which should return the user's input, e.g. 1, 2 or 3 if you offer 3 choices).

Ad
source: stackoverflow.com
Ad