Filter by property

schneck picture schneck · Jul 30, 2009 · Viewed 53.6k times · Source

Is it possible to filter a Django queryset by model property?

i have a method in my model:

@property
def myproperty(self):
    [..]

and now i want to filter by this property like:

MyModel.objects.filter(myproperty=[..])

is this somehow possible?

Answer

Glenn Maynard picture Glenn Maynard · Jul 30, 2009

Nope. Django filters operate at the database level, generating SQL. To filter based on Python properties, you have to load the object into Python to evaluate the property--and at that point, you've already done all the work to load it.