【问题标题】:How to pass parameteres to get_queryset method of django models manager?如何将参数传递给 django 模型管理器的 get_queryset 方法?
【发布时间】:2014-06-12 10:36:13
【问题描述】:

我正在尝试关注这个documentation。我想以某种方式将参数传递给 get_queryset,但不知道如何。下面的演示不工作。

class ContextAwareNotificationsManager(models.Manager):

    def get_queryset(self, user):
        return super(ContextAwareNotificationsManager, self).get_queryset(user).filter((
            Q(kind='SYSTEM') | Q(kind='TODO', status=False)
        ), content_type__name__in=['publisher', 'site', 'channel']).order_by('-date')

【问题讨论】:

  • @Matt:根据documentation 的说法,这些天来,覆盖get_queryset 似乎是正确的做法。只是在这种特定情况下可能不是。

标签: django django-models django-managers


【解决方案1】:

这应该是你要找的:

class ContextAwareNotificationsManager(models.Manager):

    def filter_user(self, user):
        return super(ContextAwareNotificationsManager, self).get_query_set().filter(...)

【讨论】:

    猜你喜欢
    • 2019-12-29
    • 2011-08-22
    • 1970-01-01
    • 1970-01-01
    • 2021-09-02
    • 2010-11-27
    • 1970-01-01
    • 2021-11-05
    • 1970-01-01
    相关资源
    最近更新 更多