小编典典

Django,从模型方法查询过滤

django

我有以下模型:

def Foo(Models.model):
    size = models.IntegerField()
    # other fields

    def is_active(self):
         if check_condition:
              return True
         else:
              return False

def Bar(Models.model):
     foo = models.ForeignKey("Foo")
     # other fields

现在,我要查询具有活动Foo的Bar:

Bar.objects.filter(foo.is_active())

我收到诸如以下错误

SyntaxError at /
('non-keyword arg after keyword arg'

我该如何实现?


阅读 440

收藏
2020-04-02

共1个答案

小编典典

你不能查询模型方法或属性。你可以在查询中使用其中的条件,也可以使用列表推导或Genex在Python中进行过滤。

2020-04-02