简体   繁体   中英

Django: How to prompt user during migration?

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.

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 (eg 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, eg 1, 2 or 3 if you offer 3 choices).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM