如何为用户提供选择页面中图像的w集合的选项?

时间:2018-11-30 15:29:23

标签: wagtail

我正在寻找一种方法来显示w集合的列表作为页面中的字段(就像上载图像时显示的一样)。用户可以选择一个集合,而我可以以编程方式将图像过滤到选定的集合。我对w还是很陌生,不确定如何在代码中实现。

预先感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

因此,有两种方法可以执行此操作。第一种也是最不理想的方法是将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对答案has a link to another solution that's much more elegant than mine.的评论

答案 1 :(得分:0)

我已经按照README.md上的说明使用wagtail-generic-chooser并使用了ag核心的Collection模型代替People来做到这一点。

相关问题