按属性过滤 [英] Filter by 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=[..])
这是不可能的吗?
推荐答案
不, Django过滤器在数据库级别运行,生成SQL。要基于Python属性进行过滤,您必须将对象加载到Python中以评估该属性 - 此时,您已经完成了所有的工作。
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.
这篇关于按属性过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文