Data migration of image model

后端 未结 1 1585
春和景丽
春和景丽 2021-01-06 02:29

Hi this is mostly a copy paste of a question asked in Google groups:

Thanks to Wagtail docs, I was able to understand how to build a custom image model, BUT, as I h

1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-06 03:02

    I recently did this same migration to my own customised model, however, we were not actively using Tags so I did not worry about transferring tags across. This migration does not delete the original Image database records as I wanted to keep them just in case.

    Step 1 - Create Model app_name/models.py

    from django.db import models
    from wagtail.wagtailimages.models import (
        Image, AbstractImage, AbstractRendition)
    
    
    class ExtendedImage(AbstractImage):
        caption = models.CharField(max_length=255, blank=True)
    
        admin_form_fields = Image.admin_form_fields + (
            'caption',
        )
    
    
    class ExtendedRendition(AbstractRendition):
        image = models.ForeignKey(ExtendedImage, related_name='renditions')
    
        class Meta:
            unique_together = (
                ('image', 'filter_spec', 'focal_point_key'),
            )
    

    Step 2 - Run Migrations Looks like you may have done this, it creates the customised model

    1. $python manage.py makemigrations
    2. $python manage.py migrate

    Step 3 - Create Custom Data Migration

    1. $python manage.py makemigrations --empty app_name

    2. Edit that file as below (see comments inline)

    ```

    from __future__ import unicode_literals
    
    from django.db import migrations
    
    # This only COPIES images from the existing model to the new one
    # to reverse during testing - run
    # ./manage.py migrate main 0036_auto_20170524_1811 (replace with file name of previous migration)
    
    
    def forwards_func(apps, schema_editor):
        # We get the model from the versioned app registry;
        wagtail_image_model = apps.get_model('wagtailimages', 'Image')
        extended_image_model = apps.get_model('main', 'ExtendedImage')
        db_alias = schema_editor.connection.alias
        # Get images
        images = wagtail_image_model.objects.using(db_alias).all()
        new_images = []
        for image in images:
            new_images.append(extended_image_model(
                id=image.id,
                title=image.title,
                file=image.file,
                width=image.width,
                height=image.height,
                created_at=image.created_at,
                focal_point_x=image.focal_point_x,
                focal_point_y=image.focal_point_y,
                focal_point_width=image.focal_point_width,
                focal_point_height=image.focal_point_height,
                file_size=image.file_size,
                # image=test_image.caption,
                collection=image.collection,
                # tags=image.tags, # does not copy over
                uploaded_by_user=image.uploaded_by_user,
            ))
        # Create images in new model
        extended_image_model.objects.using(db_alias).bulk_create(new_images)
        # Leave all images in previous model
    
    
    def reverse_func(apps, schema_editor):
        # We get the model from the versioned app registry;
        extended_image_model = apps.get_model('main', 'ExtendedImage')
        db_alias = schema_editor.connection.alias
        # Delete all images created in the new model
        extended_image_model.objects.using(db_alias).all().delete()
    
    
    class Migration(migrations.Migration):
    
        dependencies = [
            ('main', '0036_auto_20170524_1811'), # Django will create this part
        ]
    
        operations = [
            migrations.RunPython(forwards_func, reverse_func),
        ]
    

    ```

    Step 4 - Update Settings

    WAGTAILIMAGES_IMAGE_MODEL = 'my_app.ExtendedImage'

    Test this along the way and when you are ready you can delete the original image database rows if want.

    ** Note about Postgres One issue we ran into was Postgres did not like me migrating things to the primary key, we had to run a SQL query to reset the current key to the max + 1

    0 讨论(0)
提交回复
热议问题