Filtering by Django Model Properties
Can you filter a Django queryset based on a model property? Imagine you have a model property defined as follows:
<code class="python">@property def myproperty(self): # ...</code>
You want to filter the queryset using this property, something like:
<code class="python">MyModel.objects.filter(myproperty=[...])</code>
Is this achievable?
Answer
Unfortunately, it's not possible to filter directly on model properties in Django. Django filters operate at the database level, generating SQL. To evaluate Python properties, you must bring the object into Python first. However, this approach defeats the purpose of database filtering.
The above is the detailed content of Can You Filter Django Querysets Based on Model Properties?. For more information, please follow other related articles on the PHP Chinese website!