【问题标题】:How can I customise the queryset in SnippetChooserPanel within Wagtail?如何在 Wagtail 的 SnippetChooserPanel 中自定义查询集?
【发布时间】: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 介绍了如何使用 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()

【问题讨论】:

    标签: wagtail


    【解决方案1】:

    如果您使用的是 Django 2.2/Wagtail 2.5,您可以尝试创建一个 proxy model 并在模型上定义 first manager 以返回您需要的查询集。我已经在 Wagtail 中使用了带有代理模型的 some success,尽管 Wagtail 需要做更多的工作才能让代理模型始终正常工作。

    【讨论】:

    • @Matt 只是想知道您是否尝试过此解决方案,如果尝试过,它是否有效。
    • 是的,它运行良好,非常干净的解决方案。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2020-07-19
    • 1970-01-01
    相关资源
    最近更新 更多