【问题标题】:How to give user option to select a wagtail collection of images in page?如何让用户选择在页面中选择图像的鹡鸰集合?
【发布时间】:2018-11-30 15:29:23
【问题描述】:

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

提前感谢您的帮助。

【问题讨论】:

标签: wagtail


【解决方案1】:

所以有几种方法可以做到这一点。第一种,可能也是最不理想的方法是将 Collection 注册为 sn-p 并使用 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.的评论

【讨论】:

  • 谢谢,@kalob-taulien。我在我的开发环境中实现了代码,它对我有用。
  • 我尝试了@gasman 方法,但没有成功。在我阅读this stack overflow 的答案后,我明白了,Django 不允许动态加载选项。
【解决方案2】:

我已经成功地使用wagtail-generic-chooser 做到了这一点,只是按照README.md 上的说明进行操作,并使用 wagtail 核心 Collection 模型而不是 People。

【讨论】:

    猜你喜欢
    • 2018-07-29
    • 1970-01-01
    • 2017-08-16
    • 1970-01-01
    • 2021-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多