g:筛选InlinePanel ForeignKey的结果 [英] Wagtail: Filter results of an InlinePanel ForeignKey

查看:81
本文介绍了g:筛选InlinePanel ForeignKey的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些模型允许我为一个工具建立多个人工编辑器:

These models allow me to establish multiple human "editors" for a tool:

class ToolPageEditors(models.Model):
    person = models.ForeignKey('people.UserProfile')
    page = ParentalKey('ToolPage', related_name='toolpage_editors')


class ToolPage(BaseAsset):
    content_panels = BaseAsset.content_panels + [
        InlinePanel('toolpage_editors', label="Tool Editors")
    ]

但是每个 ToolPageEditors 实例都是一个下拉列表,拥有3,000多个用户。我想将该下拉菜单的内容限制为给定组中的人员。我知道如何通过覆盖admin表单在Django中执行此操作,但是在弄清楚如何在Wagtail中完成操作时遇到了麻烦。

But then each ToolPageEditors instance is a dropdown with more than 3,000 users. I'd like to limit the contents of that dropdown to people in a given group. I know how to do this in Django by overriding the admin form, but am having trouble figuring out how to accomplish it in Wagtail.

建议?谢谢。

更新:

密钥是 limit_choices_to 。对该类进行如下修改,并且可以正常工作:

The key is limit_choices_to. Modified the class as follows and it works:

class ToolPageManagers(models.Model):
    def get_tool_editors():
        g = Group.objects.get(name='Tool Editors')
        return {'groups__in': [g, ]}

    person = models.ForeignKey('people.UserProfile',  limit_choices_to=get_tool_editors)
    page = ParentalKey('ToolPage', related_name='toolpage_editors')


推荐答案

答案是对原始问题的更新,并在下面粘贴了一些文档链接。

您可以通过kwarg limit_choices_to

You can limit the available choices on a Django model's foreign key relationship by the kwarg limit_choices_to.

注意:此

将会为关系增加一个限制,而不仅仅是UI中可用的选择。 ode

class ToolPageManagers(models.Model):
    def get_tool_editors():
        g = Group.objects.get(name='Tool Editors')
        return {'groups__in': [g, ]}

    person = models.ForeignKey('people.UserProfile',  limit_choices_to=get_tool_editors)
    page = ParentalKey('ToolPage', related_name='toolpage_editors')

有关Wagtail的 InlinePanel 的更多信息,请参见Wagtail文档-> https://docs.wagtail.io/zh/latest/reference/pages/panels.html#inline-panels

For more information about Wagtail's InlinePanel see the Wagtail docs - https://docs.wagtail.io/en/latest/reference/pages/panels.html#inline-panels

这篇关于g:筛选InlinePanel ForeignKey的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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