Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django, query filtering from model method

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?

like image 447
Hellnar Avatar asked Feb 16 '10 22:02

Hellnar


1 Answers

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.

like image 129
Ignacio Vazquez-Abrams Avatar answered Sep 28 '22 04:09

Ignacio Vazquez-Abrams