How to give user option to select a wagtail collection of images in page?
我正在寻找一种将w集合的列表显示为页面中的字段的方法(就像上载图像时显示的一样)。 用户可以选择一个集合,而我可以以编程方式将图像过滤到选定的集合。 我还是不熟悉wagtail的人,而且不确定如何在代码中实现此功能。
预先感谢您的帮助。
因此,有两种方法可以实现此目的。 第一种也是最不理想的方法是将Collection注册为代码段并使用
1 2 3 4 5 6 | """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,就像这样(注意,这都是未经测试的代码)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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对答案的评论与另一个比我优雅得多的解决方案链接。
我已经按照