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

标签 wagtail

假设我有这样一个模型:

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

    owner = models.ForeignKey(User)

    panels = [
        SnippetChooserPanel('owner'),
    ]

在 Wagtail 管理员中,我想要 snippet chooser panel排除某些所有者,例如Steve 不能相信三明治。如何自定义使用的查询集?

documentation关于如何使用 PageChooserPanelDocumentChooserPanelImageChooserPanel 执行此操作,但不是 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,您可以尝试创建一个 proxy model并定义 first manager在模型上返回您需要的查询集。我有 some success在 Wagtail 中使用代理模型,尽管 Wagtail 需要做更多的工作才能使代理模型始终正常工作。

关于wagtail - 如何在 Wagtail 中自定义 SnippetChooserPanel 中的查询集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56915888/

相关文章:

django - 如何从 HomePage(Page) 重定向到您的自定义应用程序模型

django - 鹡鸰式的评论方法

django - 如何覆盖 wagtail 管理登录页面

django - 有什么方法可以在 Wagtail 管理员的列表页面上显示字段吗?

python - 我无法仅在模板中将 bool 字段设置为 true 时显示帖子

python - Wagtail-ModelTranslation 模板片段缓存

python - 如何使 Wagtail 搜索不区分大小写

django - 将标签放入 StructBlock

python - 鹡鸰建立第一个站点