自定义图像模型在管理 UI 中显示为纯 html 选择

问题描述 投票:0回答:1

我创建了一个自定义图像模型来添加图像属性。我只是按照 Wagtail 文档进行操作:https://docs.wagtail.org/en/stable/advanced_topics/images/custom_image_model.html

我的models.py设置后没有变化

WAGTAILIMAGES_IMAGE_MODEL = 'my_images.CustomImage'

模型.py

from django.db import models
from modelcluster.fields import ParentalKey
from wagtail.admin.panels import FieldPanel

from wagtail.models import Page
from people_and_places.models import Person

class HomePage(Page):
    image = models.ForeignKey(
        'wagtailimages.Image', on_delete=models.SET_NULL, related_name='+', null=True, blank=True,
    )
    contact = models.ForeignKey(
        Person, on_delete=models.SET_NULL, null=True, blank=True,
        verbose_name="Some Person", help_text="Just testing the ChooserWidget"
    )

    content_panels = Page.content_panels + [
                FieldPanel('image'),
                FieldPanel('contact'),
    ]

一切正常,包括图像管理页面。尝试在管理 UI 中编辑页面时除外。 Wagtail 仅渲染纯 html 选择元素,而不是 Wagtail 图像模态。

使用自定义图像模型并仍然获得标准 Wagtail 图像模态的所有好处的最简单(或至少是 Pythonic)方法是什么?我怎样才能获得默认图像模态而不是html选择元素

我觉得问这个问题有点愚蠢,因为对我来说,我似乎不太可能是唯一一个为此苦苦挣扎的人。但我找不到任何有用的东西。至少对于当前版本的 Wagtail 来说是这样。 我发现了一些关于 ImageChooserPanel 的文档,但现在似乎已经过时了。

我什至尝试创建自己的 ChooserViewSet 但没有成功。 在我的 ImageApps 视图中。py

from wagtail.admin.viewsets.chooser import ChooserViewSet

class MyImageChooserViewSet(ChooserViewSet):
    # The model can be specified as either the model class or an "app_label.model_name" string;
    # using a string avoids circular imports when accessing the StreamField block class (see below)
    model = "images.ImageWithCopyright"
    icon = "user"
    choose_one_text = "Choose an image"
    choose_another_text = "Choose another image"
    edit_item_text = "Edit this image"
    form_fields = ["image", "copyright"]  # fields to show in the "Create" tab

my_image_chooser_viewset = MyImageChooserViewSet("my_image_chooser")

和 wagtail_hooks.py

from wagtail import hooks

from .views import my_image_chooser_viewset

@hooks.register("register_admin_viewset")
def register_viewset():
    return my_image_chooser_viewset

但我仍然坚持选择元素。

wagtail wagtail-admin
1个回答
0
投票

HomePage 模型上的

image
字段仍然指向 Wagtail 的内置 Image 模型。这不再是活动图像模型(因为您已通过
WAGTAILIMAGES_IMAGE_MODEL
设置覆盖它),因此它不会接收图像选择器。

外键应更新为指向您的自定义图像模型:

class HomePage(Page):
    image = models.ForeignKey(
        'images.ImageWithCopyright', on_delete=models.SET_NULL, related_name='+', null=True, blank=True,
    )
© www.soinside.com 2019 - 2024. All rights reserved.