Transfer data from one model to another with Django South

I currently have a structure that needs to be rewritten to deal with Django-CMS

Currently, the setup is as follows

class Video(models.Model): #embed_code_or_url = models.CharField(max_length=2000) permalink = models.URLField(verify_exists=True, unique=True, max_length=255, default="http://", validators=[validate_youtube_address]) thumbnail = models.CharField(max_length=500, blank=True, null=True) # Data title = models.CharField(max_length=255, blank=True) ... class VideoPlugin(CMSPlugin): video = models.ForeignKey(Video) 

when I transfer all my fields from Video to VideoPlugin , start my schematic, I would also like to transfer all the information from the video to VideoPlugin when starting the migration.

Does anyone have an example of how this can be achieved?

Here is the start of the migration to run

 class Migration(SchemaMigration): def forwards(self, orm): # Adding field 'VideoPlugin.permalink' db.add_column('cmsplugin_videoplugin', 'permalink', self.gf('django.db.models.fields.URLField')(default='http://', unique=True, max_length=255), keep_default=False) # Adding field 'VideoPlugin.thumbnail' db.add_column('cmsplugin_videoplugin', 'thumbnail', self.gf('django.db.models.fields.CharField')(max_length=500, null=True, blank=True), keep_default=False) # Adding field 'VideoPlugin.title' db.add_column('cmsplugin_videoplugin', 'title', self.gf('django.db.models.fields.CharField')(default='', max_length=255, blank=True), keep_default=False) ... 

Your help is much appreciated

+6
source share
1 answer

You create datamigration:

 $ python manage.py datamigration yourapp name_of_this_migration 

This freezes the models in your application. If another application (s) is involved in the migration process, you need to add --freeze app1 --freeze app2 , etc. On this line to include those that are in your migration.

This sets up the basic structure of the migration files for you, but the forwards and backwards migrations are empty. It is up to you to determine the logic that will transfer data from one to another. But it works like everything else in Django, except for using South ORM. For any model of your application where this migration is located, you use orm.MyModel.objects for any other application that is added with the --freeze options, you use orm['someapp.SomeModel'].objects .

Other than that, you just get / filter / create, etc. objects as usual, moving data from one to another. Obviously, your redirected transfer requires logic that moves the data where you want, and your reverse migration must have the logic necessary to restore the data to where it was originally.

You can then move back and forth in your environment to ensure that it works properly. One important note: this is just for moving data. DO NOT modify or delete any table structures in your datamigration. If you need to delete tables after moving data. Create a migration scheme after datamigration.

+15
source

Source: https://habr.com/ru/post/899962/


All Articles