Wagtail: Filter results of an InlinePanel ForeignKey

∥☆過路亽.° 提交于 2019-12-10 08:42:50

问题


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")
    ]

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.

Suggestions? Thanks.

Update:

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')

来源:https://stackoverflow.com/questions/40554215/wagtail-filter-results-of-an-inlinepanel-foreignkey

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!