如何将字段添加到默认提升面板的多字段

How to add a field to default Promote panel's Multifield

我正在尝试向 "Promote" 面板的现有 MultiField 添加一个字段。

在我的页面子类中,我设置了这个:

slug_en = models.SlugField(
        verbose_name='slug (EN)',
        allow_unicode=True,
        max_length=255,
        help_text="The name of the page as it will appear in URLs e.g http://example.com/blog/[my-slug]/"
    )

...

Page.promote_panels[0].children.insert(1, FieldPanel('slug_en'))

我尝试使用默认的 "title" 字段而不是这个自定义字段,并且确实有效。

服务器returns错误:

django.core.exceptions.FieldError: Unknown field(s) (slug_en) specified for Page

问题应该是由于某种原因,此时该字段尚未初始化。

如何将字段添加到 Page.promote_panel 成功?

您可以尝试 re-creating TabbedInterface(这就是您在编辑页面时创建选项卡的原因)。

from wagtail.admin.edit_handlers import (
    FieldPanel,
    MultiFieldPanel,
    ObjectList,
    TabbedInterface,
)
from wagtail.core.models import Page


class YourPage(Page):
    ....


YourPage.edit_handler = TabbedInterface(
    [
        ObjectList(YourPage.content_panels, heading="Content"),
        ObjectList(
            [
                MultiFieldPanel([
                    FieldPanel('slug'),
                    FieldPanel('seo_title'),
                    FieldPanel('show_in_menus'),
                    FieldPanel('search_description'),
                    Field("custom_field_1"),
                    Field("custom_field_2"),
                ], 'Common page configuration'),
            ],
            heading="Promote",
        ),
        ObjectList(YourPage.settings_panels, heading="Settings"),
    ],
)

如果你有兴趣了解更多,我有一个 video on this subject on YouTube

希望对您有所帮助!