views:

163

answers:

2

Hello, I have these models:

def Foo(Models.model):
    size = models.IntegerField()
    ....some other vars...

    def is_active(self):
         if ...checks something here...
              return True
         else:
              return False

def Bar(Models.model):
     foo = models.ForeignKey("Foo")
     ....some other vars...

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?

+1  A: 

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.

Ignacio Vazquez-Abrams
+3  A: 

You can't filter on methods, however if the is_active method on Foo checks an attribute on Foo, you can use the double-underscore syntax like Bar.objects.filter(foo__is_active_attribute=True)

Gabriel Hurley
I am getting: Cannot resolve keyword 'is_active_attribute' into field Choices are: .... all variable names listed here
Hellnar
Yeah, that's why I said "if the is_active method on Foo checks an attribute...". Filtering only works on database fields, not methods. So if is_active *is checking some attribute inside the method* you can check the same thing in your filter. If it's doing something more complicated, you're outta luck and need to find a different solution. If you want a specific answer, post the real code for Foo.is_active(). Otherwise all you'll get is pseudo-code solutions that you shouldn't expect to work verbatim.
Gabriel Hurley