Django, query filtering from model method

Hellnar picture Hellnar · Feb 16, 2010 · Viewed 42.3k times · Source

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?

Answer

Ignacio Vazquez-Abrams picture Ignacio Vazquez-Abrams · Feb 16, 2010

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.