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

0 votes
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 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.
Aug 12, 2020 in Python by kartik
• 37,520 points
873 views

1 answer to this question.

0 votes

Hello @kartik,

Try this:

from south.db import db
from south.v2 import SchemaMigration
from django.db import models

class Migration(SchemaMigration):

    def forwards(self, orm):
        db.rename_table('common_cat', 'specific_cat') 

        if not db.dry_run:
             db.execute(
                "update django_content_type set app_label = 'specific' where "
                " app_label = 'common' and model = 'cat';")

    def backwards(self, orm):
        db.rename_table('specific_cat', 'common_cat')
            db.execute(
                "update django_content_type set app_label = 'common' where "
                " app_label = 'specific' and model = 'cat';")

Hope it works!!

Thank you!

answered Aug 12, 2020 by Niroj
• 82,840 points

Related Questions In Python

0 votes
0 answers
+1 vote
1 answer
0 votes
1 answer
–1 vote
2 answers
0 votes
1 answer

How to temporarily disable a foreign key constraint in MySQL?

Hello @kartik, To turn off foreign key constraint ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,840 points
2,510 views
0 votes
1 answer

How do I use Django templates without the rest of Django?

Hello @kartik, Let's say you have this important ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,840 points
1,586 views
0 votes
1 answer

How do I add a link from the Django admin page of one object to the admin page of a related object?

Hello @kartik, Set show_change_link to True (False by default) in your inline ...READ MORE

answered Jun 12, 2020 in Python by Niroj
• 82,840 points
10,654 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP