如何在 Wagtail 中自定义 SnippetChooserPanel 中的查询集?

How can I customise the queryset in SnippetChooserPanel within Wagtail?

假设我有这样一个模型:

class Sandwich(models.Model):
    """
    Food-like things stacked horizontally.
    """

    owner = models.ForeignKey(User)

    panels = [
        SnippetChooserPanel('owner'),
    ]

在 Wagtail 管理员中,我希望 snippet chooser panel 排除某些 owners,例如史蒂夫 不能相信三明治。如何自定义使用的查询集?

关于如何使用 PageChooserPanelDocumentChooserPanelImageChooserPanel,有 documentation,但没有 SnippetChooserPanel

编辑

@dan-swains 的回答非常有效,即使使用自定义 User 模型也是如此。

@register_snippet
class User(AbstractUser):
    """
    My custom `User` model…
    """

class NoSteveManager(models.Manager):
    def get_queryset(self):
        """
        Anybody who is not called `Steve`.
        """
        return super().get_queryset().exclude(first_name__iexact='steve')

@register_snippet
class SandwichEater(User):
    """
    Only people who are not a `Steve` are considered sandwich eaters.
    """
    class Meta:
        proxy = True

    objects = NoSteveManager()

如果您使用的是 Django 2.2/Wagtail 2.5,您可以尝试在 Wagtail 中创建一个带有代理模型的 proxy model and define the first manager on the model to return the queryset that you need. I've had some success 尽管 Wagtail 需要做更多的工作才能使代理模型始终正常工作。