Django:使用方法而不是属性过滤查询集?

时间:2015-03-23 13:24:49

标签: python django django-queryset

我正在努力让我的Django应用程序中的所有忠实客户。为此,我试图使用方法而不是属性来过滤查询集,我不知道如何做到这一点。 编辑:使用Kedar建议,我尝试使用@property装饰器

class Client(models.Model):

    @property
    def is_loyal(self):
        # ...
        # Return True or False

    @classmethod
    def get_loyal_clients_since(cls):

        # Method #1: works but clearly a workaround
        base_clients = cls.some_method_to_retrieve_special_clients()
        clients_list = [c.pk for c in base_clients if c.is_loyal()]
        clients = cls.objects.filter(pk__in=clients_list)

        # Method #2: does not work
        clients = cls.some_method_to_retrieve_special_clients() \
                  .filter(is_loyal=True)

错误:Cannot resolve keyword 'is_loyal' into field.

0 个答案:

没有答案
相关问题