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.
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!