Move Django model from one app to another [duplicate]

半腔热情 提交于 2019-11-29 06:56:25

I don't see why you need any data migration at all.

Just move the models to the new app, and add a db_table setting in the inner Meta classes to point to the old table names.

I did something similar on a smaller scale recently and this was my process:

  1. Create new app and corresponding models
  2. Update views to use new models
  3. Update unit/systems tests to make sure nothing broke (important!)
  4. Write a management command that populates the new models based on the old models
  5. Deploy code
  6. Run migration for new models
  7. Run management command script to update new models
  8. Leave old app for 1-2 weeks and when you think its all good, drop them.

The reasons why I didnt use data migration:

  1. Not familiar -- felt the task was too important to use a process I wasn't familiar with
  2. More comfortable moving data with python code then with South magic
  3. Ran into south migration issues with dependencies. Didn't want to further complicate the migrations with a data migration. This could will be a mis-founded assumption due to my unfamiliarity with the mechanics of a data migration
  4. Perhaps as a bias from point 3, I convinced myself using South purely as a schema management tool is the 'right' way to do. Creation/updating of data should be done in the Django layer using either fixtures or custom management commands

The simplest solution I could think of:

  1. Create a SchemaMigration changing the type of every foreign key to models in the old app to a primitive type (including ones internal to it);
  2. Create the new apps and their models normally;
  3. Do a data migration from the old tables to the new ones;
  4. Create another SchemaMigration, changing every primitive type to a foreign key again, now pointing to the new tables;
  5. Remove the old app from settings and drop its tables.

Laborious, yes, but would do the trick. I'd hope for a better solution though.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!