【发布时间】:2019-07-06 16:31:29
【问题描述】:
假设我有一个这样的模型:
class Sandwich(models.Model):
"""
Food-like things stacked horizontally.
"""
owner = models.ForeignKey(User)
panels = [
SnippetChooserPanel('owner'),
]
在 Wagtail 管理员中,我希望 snippet chooser panel 排除某些 owners,例如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()
【问题讨论】:
标签: wagtail