John
John

Reputation: 155

Django: Filtering foo_set via a custom property?

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.

Upvotes: 2

Views: 246

Answers (2)

Dominic Rodger
Dominic Rodger

Reputation: 99801

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.

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799044

You can only query on actual fields, not properties.

Upvotes: 3

Related Questions