如何让用户选择在页面中选择图像的 wagtail 集合? [英] How to give user option to select a wagtail collection of images in page?

查看:35
本文介绍了如何让用户选择在页面中选择图像的 wagtail 集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am looking for a way to show a list of wagtail collection as a field in a page (just like it showing when you upload an image). A user can select a collection and I can programmatically filter the images to the selected collection. I am still new to wagtail and I am not sure how should I implement this in code.

预先感谢您的帮助.

推荐答案

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

So there's a couple ways you can do this. The first, and probably the least-ideal way is to register Collection as a snippet and use a 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,就像这样(注意,这都是未经测试的代码)

And then in your model you can use a SnippetChooserPanel, like so (note, this is all untested code)

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 对答案的评论 有一个链接到另一个比我的更优雅的解决方案.

这篇关于如何让用户选择在页面中选择图像的 wagtail 集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆