How to transfer data from one model to another using the 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 reorganize the voting system in my application so that I can reuse it. However, the original application is in production, and I need to create a data migration that will take all the voices and transplant them into a separate application.

How can I get two applications to participate in the migration so that I have access to their models? Unfortunately, the original and individual applications have a model called "Vote", so I need to know about any conflicts.

+4
source share
1 answer

Have you tried db.rename_table ?

I would start by creating a migration in a new or old application 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 this does not work, you can move each element in a loop with something like this:

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

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

+5
source

All Articles