我有这些模型:
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.objects.filter(foo.is_active())我得到的错误信息如下
SyntaxError at /
('non-keyword arg after keyword arg'我如何才能做到这一点?
发布于 2010-02-17 06:22:03
不能针对模型方法或属性进行查询。在查询中使用其中的条件,或者在Python中使用列表理解或genex进行过滤。
发布于 2014-01-26 20:00:32
您还可以使用自定义管理器。然后你可以运行下面这样的代码:
Bar.objects.foo_active()你要做的就是:
class BarManager(models.Manager):
def foo_active(self):
# use your method to filter results
return you_custom_queryset查看docs。
发布于 2010-02-17 06:24:30
您不能过滤方法,但是,如果Foo上的is_active方法检查Foo上的属性,则可以使用像Bar.objects.filter(foo__is_active_attribute=True)这样的双下划线语法
https://stackoverflow.com/questions/2276768
复制相似问题