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.
Das obige ist der detaillierte Inhalt vonKönnen Sie Django-Abfragesätze basierend auf Modelleigenschaften filtern?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!