views:

37

answers:

2

Hi,

I have a model called Product with a custom property:

def _get_active(self):
    o = get_option()
    if self.date_expiration == None:
        return True
    if self.date_expiration <= o.working_month:
        return False
    return True
active = property(_get_active)

... and in one of my methods, I have this line:

products = g.product_set.filter(active__exact=True)

But despite the fact that (I think) I've correctly set up the property, the above line gives me "Cannot resolve keyword 'active' into field." What am I missing here?

Thanks in advance.

+3  A: 

You can only query on actual fields, not properties.

Ignacio Vazquez-Abrams
`for product in (x for x in g.product_set.iterator() if x.active):`
Ignacio Vazquez-Abrams
+1  A: 

Expanding on Ignacio's answer, you could just do:

products = [x for x in g.product_set.iterator() if x.active]

Though that's going to be very inefficient, since you've got to load every record in x.product_set.

Dominic Rodger