如何在编辑页面时添加 content_panels

How to add content_panels when I edit Page

我正在开发我的 wagtail 博客网站。 我想添加动态显示 SnippetChooserPanel 的功能。 当我创建一个博客编辑页面时,我想编辑 1/3 SnippetChooserPanel。 当我编辑Blog编辑页面时,我想编辑3/3 SnippetChooserPanel。

但是我无法解决...

  1. 我在 blog/models.py 中删除了 2 个 SnippetChooserPanel,"B" 和 "C"。我只能编辑 "A" SnippetChooserPanel -> 没问题。
  2. 我在 blog/wagtail_hooks.py 中添加了代码 -> 但是,SnippetChooserPanel 看不到。

是blog/models.py


content_panels = Page.content_panels + [
        MultiFieldPanel(
            [
                SnippetChooserPanel("A"),
                # SnippetChooserPanel("B"),
                # SnippetChooserPanel("C"),
            ],
            heading=_("ABC information"),
        ),
    ]

是2和blog/wagtail_hooks.py的过程。如果我添加

@hooks.register("before_edit_page")

...
...

Page.content_panels = Page.content_panels + [
        MultiFieldPanel(
            [
                SnippetChooserPanel("B"),
                SnippetChooserPanel("C"),
            ],
            heading=_("ABC more information"),
        ),
    ]

...
...

我做不好.. 有没有人可以帮助我?

我遇到了类似的问题,并找到了解决方案,尽管这可能不是理想的解决方案。

wagtail/contrib/modeladmin/options.py中,我读到:

class ModelAdmin(WagtailRegisterable):

    def get_edit_handler(self, instance, request):
        """
        Returns the appropriate edit_handler for this modeladmin class.
        edit_handlers can be defined either on the model itself or on the
        modeladmin (as property edit_handler or panels). Falls back to
        extracting panel / edit handler definitions from the model class.
        """
        if hasattr(self, 'edit_handler'):
            edit_handler = self.edit_handler
        elif hasattr(self, 'panels'):
            panels = self.panels
            edit_handler = ObjectList(panels)
        …
        return edit_handler

因此,您可以覆盖 get_edit_handler 来决定您想要什么 return。如果这是一个创建视图,实例将为空,否则,它将有一个 id。

# wagtail_hooks.py
from wagtail.contrib.modeladmin.options import ModelAdmin
from wagtail.admin.edit_handlers import ObjectList

class BlogAdmin(ModelAdmin):
    model = Blog

    def get_edit_handler(self, instance, request):
        panels = instance.create_panels
        if instance.pk:
            panels += instance.edit_panels
        return ObjectList(panels)


# models.py

class Blog(Page):

    create_panels = [
        SnippetChooserPanel("A"),
    ]
    edit_panels = [
        SnippetChooserPanel("B"),
        SnippetChooserPanel("C"),
    ]

就是这样。