Search code examples
djangodjango-queryset

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?


Solution

  • 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.