图像模型的数据迁移



嗨,这主要是谷歌小组中一个问题的复制粘贴:

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

非常感谢问候

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

  • 我创建了一个自定义图像模型,以便在图像端点上将图像URL直接提供给Wagtail API(例如:https://github.com/wagtail/wagtaildemo/blob/api-tweaks/demo/models.py#L784-L795)
  • 正如wagtail文档中所解释的,需要进行数据迁移。但由于我从未经历过这个过程,我只想确保以正确的方式进行

这肯定是特定于wagtail的,为什么我可能省略了一些细节。抱歉。由于我没有太多的声誉,我不能提供超过2个链接一个单一的后

我最近对我自己的定制模型进行了同样的迁移,但是,我们没有积极使用标签,所以我不担心跨标签传输。这次迁移不会删除原始的Image数据库记录,因为我想保留它们以防万一。

步骤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. 按如下方式编辑该文件(见注释行)

``

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

最新更新