Django,从模型方法查询过滤

发布于 2021-01-29 18:13:17

我有以下模型:

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'

我该如何实现?

关注者
0
被浏览
50
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

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



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看