Django migration strategy for renaming a model and relationship fields

后端 未结 12 1487
一个人的身影
一个人的身影 2020-11-28 00:54

I\'m planning to rename several models in an existing Django project where there are many other models that have foreign key relationships to the models I would like to rena

相关标签:
12条回答
  • 2020-11-28 01:31

    For Django 1.10, I managed to change two model class names (including a ForeignKey, and with data) by simply running Makemigrations, and then Migrate for the app. For the Makemigrations step, I had to confirm that I wanted to change the table names. Migrate changed the names of the tables without a problem.

    Then I changed the name of the ForeignKey field to match, and again was asked by Makemigrations to confirm that I wanted to change the name. Migrate than made the change.

    So I took this in two steps without any special file editing. I did get errors at first because I forgot to change the admin.py file, as mentioned by @wasibigeek.

    0 讨论(0)
  • 2020-11-28 01:32

    I am using Django version 1.9.4

    I have follow the following steps:-

    I have just rename the model oldName to NewName Run python manage.py makemigrations. It will ask you for Did you rename the appname.oldName model to NewName? [y/N] select Y

    Run python manage.py migrate and it will ask you for

    The following content types are stale and need to be deleted:

    appname | oldName
    appname | NewName
    

    Any objects related to these content types by a foreign key will also be deleted. Are you sure you want to delete these content types? If you're unsure, answer 'no'.

    Type 'yes' to continue, or 'no' to cancel: Select No
    

    It rename and migrate all existing data to new named table for me.

    0 讨论(0)
  • 2020-11-28 01:33

    I upgraded Django from version 10 to version 11:

    sudo pip install -U Django
    

    (-U for "upgrade") and it solved the problem.

    0 讨论(0)
  • 2020-11-28 01:36

    I needed to rename a couple of tables. But only one model rename was noticed by Django. That happened because Django iterates over added, then removed models. For each pair it checks if they're of the same app and have identical fields. Only one table had no foreign keys to tables to be renamed (foreign keys contain model class name, as you remember). In other words, only one table had no field changes. That's why it was noticed.

    So, the solution is to rename one table at a time, changing model class name in models.py, possibly views.py, and making a migration. After that inspect your code for other references (model class names, related (query) names, variable names). Make a migration, if needed. Then, optionally combine all these migrations into one (make sure to copy imports as well).

    0 讨论(0)
  • 2020-11-28 01:38

    If you are using a good IDE like PyCharm you can right click on the model name and do a refactor -> rename. This saves you the trouble of going through all your code that references the model. Then run makemigrations and migrate. Django 2+ will simply confirm name change.

    0 讨论(0)
  • 2020-11-28 01:43

    Just wanted to confirm and add upon ceasaro comment. Django 2.0 seems to do this automatically now.

    I'm on Django 2.2.1, all I had to do what to rename the model and run makemigrations.

    Here it asks if I had renamed the specific class from A to B, i chose yes and ran migrate and all seems to work.

    Note I did not rename the old model name in any files inside the project/migrations folder.

    0 讨论(0)
提交回复
热议问题