Python Django 查询集过滤器 GT、LT、GTE、LTE 返回完整的对象列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24211293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django queryset filter GT, LT, GTE, LTE returns full object list
提问by apardes
I'm trying to filter objects in my database using .filter(field__lte = parameter)however it just returns ALL objects and does not filter any out. I have even set the parameter to well above any value that is stored in the database and all objects are still returned.
我正在尝试使用过滤器过滤我的数据库中的对象,.filter(field__lte = parameter)但是它只返回所有对象并且不会过滤掉任何对象。我什至将参数设置为远高于存储在数据库中的任何值,并且仍然返回所有对象。
>> all_objects = Ranked.objects.all()
>> filtered = all_objects.filter(score__lte = 100) #The max possible score is 100
>> len(filtered)
87 #Every object in the db
The field in the database that I am querying against is an IntegerField.
我查询的数据库中的字段是IntegerField.
Am I doing something wrong here? Thanks for your help.
我在这里做错了吗?谢谢你的帮助。
回答by alecxe
You are saying that The max possible score is 100. By using score__lte=100, you are filtering all objects with scoreless than or equal to100 - which is every object in the table by your own definition.
你是这么说的The max possible score is 100。通过使用score__lte=100,您将过滤score小于或等于100 的所有对象- 这是您自己定义的表中的每个对象。
回答by Aamir Adnan
As you said max possible score is 100so it will always return all objects because ltemeans return all objects whose score is either less than or equal to 100. You might need ltlookup which means just return those objects whose score is less than 100:
正如您所说的最大可能分数是100这样,它将始终返回所有对象,因为这lte意味着返回分数小于或等于 的所有对象100。您可能需要lt查找,这意味着只返回分数小于 的对象100:
filtered = all_objects.filter(score__lt=100)

