How do I migrate a model out of one django app and into a new one?

Viewed 39982

I have a django app with four models in it. I realize now that one of these models should be in a separate app. I do have south installed for migrations, but I don't think this is something it can handle automatically. How can I migrate one of the models out of the old app into a new one?

Also, keep in mind that I'm going to need this to be a repeatable process, so that I can migrate the production system and such.

7 Answers

Models aren't very tightly coupled to apps, so moving is fairly simple. Django uses the app name in the name of the database table, so if you want to move your app you can either rename the database table via an SQL ALTER TABLE statement, or - even simpler - just use the db_table parameter in your model's Meta class to refer to the old name.

If you've used ContentTypes or generic relations anywhere in your code so far, you will probably want to rename the app_label of the contenttype pointing at the model that's moving, so that existing relations are preserved.

Of course, if you don't have any data at all to preserve, the easiest thing to do is to drop the database tables completely and run ./manage.py syncdb again.

Related