How to rename a Django app and migrate data from an app to the other

Viewed 8050

I have a Django app named app1 with models and migrations files. I renamed this app to app2 and I fixed all imports, urls etc... I now have a problem with migrations files and data in tables.

How can I write migrations with the correct way to ensure:

  • New installation => create the new tables
  • Update old versions => create new tables, move data, remove old tables

Note 1: there is several tables with many Foreign Keys.

Here is my progress so far and I am not sure if I am on the good way:

  • I removed all older migrations
  • I ran python manage.py makemigrations to generate new migrations files

After these 2 steps, I can install my application but I still have problems with old version.

Question: What is the best way to migrate data?

Note 2: I don't use South.

3 Answers

This is eloquently answered in this blog post.

The bullet points are:

  1. Rename the folder of the application you want to update.
  2. Update any and import statements to the folder you updated.
  3. Update entries for the django_content_type table to refer to the application's app_label.
  4. Update the table names of any models you haven't explicitly set the table name of. These table names inferred by the application name and need to be updated.
  5. Update entries for the django_migrations table and update the reference for each migration by setting the app field your new app label.
  6. Update any namespaced folder names that are within your /static or /templates folder. For example, you might have ./foo_app/templates/foo_app/index.html and it should be updated to ./bar_app/templates/bar_app/index.html.
Related