【问题标题】:Data migration of image model图像模型的数据迁移
【发布时间】:2017-01-30 08:31:28
【问题描述】:

您好,这主要是复制粘贴在 Google 群组中提出的问题:

感谢 Wagtail 文档,我能够了解如何构建自定义图像模型,但是,由于我有一个包含 500 多个内容的网站,我不想因为糟糕的数据迁移而搞砸整个事情. 事实上,我不确定我应该在这里使用哪个迁移操作。 我想我应该这个:https://docs.djangoproject.com/en/1.8/ref/migration-operations/#altermodeltable 有人可以证实这一点吗?

非常感谢 问候

编辑:提供有关迁移的一些详细信息

这肯定是鹡鸰特有的,为什么我可能省略了一些细节。对此感到抱歉。而且因为我没有太多的声誉,我不能在一个帖子中提供超过 2 个链接 ????

【问题讨论】:

  • 毫无疑问,您的问题没有在 Google 网上论坛中得到解答,因为您没有提供足够的信息。你想做什么?迁移是为了什么?为什么你会认为它会“搞砸整个事情”?
  • 我刚刚提供了更多细节。抱歉,如果它太特定于 wagtail,我可以删除其他标签以避免不知道 wagtail 的人被我的问题所困扰。
  • 顺便说一下,这就是我“从 Google 群组复制粘贴”的原因:groups.google.com/forum/#!topic/wagtail/ZFkK_tjZj1Q 我没有得到任何答案的原因之一是 Wagtail 的 Google 群组支持频道被放弃了鹡鸰维护者。

标签: wagtail


【解决方案1】:

我最近做了同样的迁移到我自己的定制模型,但是,我们没有积极使用标签,所以我不担心跨标签转移。此迁移不会删除原始图像数据库记录,因为我想保留它们以防万一。

第 1 步 - 创建模型 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'),
        )

第 2 步 - 运行迁移 看起来你可能已经这样做了,它会创建自定义模型

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

第 3 步 - 创建自定义数据迁移

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

  2. 如下编辑该文件(参见 cmets 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),
    ]

```

第 4 步 - 更新设置

WAGTAILIMAGES_IMAGE_MODEL = 'my_app.ExtendedImage'

在此过程中进行测试,准备好后,您可以根据需要删除原始图像数据库行。

** 关于 Postgres 的注意事项 我们遇到的一个问题是 Postgres 不喜欢我将东西迁移到主键,我们必须运行 SQL 查询来将当前键重置为 max + 1

【讨论】:

  • 嘿!非常感谢您的回答,这有点晚了,因为我终于能够使用不同的图像模型而无需迁移数据。这就是为什么我没有回答这个问题,因为我终于不再需要迁移了,但是我会在需要时立即尝试您的解决方案,这似乎很好解释!
猜你喜欢
  • 2012-01-29
  • 1970-01-01
  • 1970-01-01
  • 2012-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多