如何为 select 页面中的鹡鸰图像集提供用户选项?

How to give user option to select a wagtail collection of images in page?

我正在寻找一种方法来将鹡鸰系列列表显示为页面中的一个字段(就像上传图片时显示的一样)。用户可以 select 一个集合,我可以通过编程将图像过滤到 selected 集合。我还是 wagtail 的新手,我不确定我应该如何在代码中实现它。

预先感谢您的帮助。

所以有几种方法可以做到这一点。第一种可能也是最不理想的方法是将 Collection 注册为一个片段并使用 SnippetChooserPanel.

"""Register Collection snippet."""
from wagtail.snippets.models import register_snippet
from wagtail.core.models import Collection

# Register Collections as Snippets so we can use the SnippetChooserPanel to select a collection
register_snippet(Collection)

然后在您的模型中您可以使用 SnippetChooserPanel,就像这样(注意,这是所有未经测试的代码)

from django.db import models
from wagtail.core.models import Page

class CustomPage(Page):

    # ...
    collection = models.ForeignKey(
        'wagtailcore.Collection',
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name='+',
    )

    content_panels = Page.content_panels + [
        # ...
        SnippetChooserPanel('collection'),
    ]

@gasman's comment on the answer has a link to another solution that's much more elegant than mine.

我只是按照 README.md 上的说明使用 wagtail-generic-chooser 并使用 wagtail 核心集合模型而不是 People 成功地做到了这一点。