开发者

How do I migrate data from one model to another using South in Django?

I created a Django app that had its own internal voting system and a model called Vote to track it. I want to refactor the voting system into its own app so I can reuse it. However, the original app is in production and I need to开发者_StackOverflow中文版 create a data migration that will take all the Votes and transplant them into the separate app.

How can I get two apps to participate in a migration so that I have access to both their models? Unfortunately, the original and separate apps both have a model named Vote now, so I need to be aware of any conflicts.


Have you tried db.rename_table?

I would start by creating a migration in either the new or old app that looks something like this.

class Migration:

    def forwards(self, orm):
        db.rename_table('old_vote', 'new_vote')    

    def backwards(self, orm):
        db.rename_table('new_vote', 'old_vote')

If that does not work you can migrate each item in a loop with something along these lines:

def forwards(self, orm):
    for old in orm['old.vote'].objects.all():
        # create a new.Vote with old's data
models = {
    'old.vote' = { ... },
    'new.vote' = { ... },
}

Note: You must use orm[...] to access any models outside the app currently being migrated. Otherwise, standard orm.Vote.objects.all() notation works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜