Django,从模型方法查询过滤 [英] Django, query filtering from model method

查看:88
本文介绍了Django,从模型方法查询过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这些模型:

  def Foo(Models.model):
size = models.IntegerField )
#其他字段

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

def Bar(Models.model):
foo = models.ForeignKey(Foo)
#其他字段

现在我想查询那些有活跃Foo的条形码:

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

我收到错误,如<

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $

如何实现?

解决方案

不能查询模型方法或属性,或者在查询中使用其中的条件,或者使用列表解析或genex过滤Python。


I have these models:

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

Now I want to query Bars that are having active Foo's as such:

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

I am getting error such as

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

How can I achieve this?

解决方案

You cannot query against model methods or properties. Either use the criteria within it in the query, or filter in Python using a list comprehension or genex.

这篇关于Django,从模型方法查询过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆