假设我有这样一个模型:
class Sandwich(models.Model):
"""
Food-like things stacked horizontally.
"""
owner = models.ForeignKey(User)
panels = [
SnippetChooserPanel('owner'),
]
在 Wagtail 管理员中,我想要 snippet chooser panel排除某些所有者
,例如Steve 不能相信三明治。如何自定义使用的查询集?
有documentation关于如何使用 PageChooserPanel
、DocumentChooserPanel
和 ImageChooserPanel
执行此操作,但不是 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/