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.
我在这里做错了吗?谢谢你的帮助。
2 个解决方案
#1
7
As you said max possible score is 100
so it will always return all objects because lte
means return all objects whose score is either less than or equal to 100
. You might need lt
lookup which means just return those objects whose score is less than 100
:
正如你所说的最大可能得分为100所以它总会返回所有对象,因为lte意味着返回得分小于或等于100的所有对象。你可能需要lt lookup,这意味着只返回那些得分低于100的对象:
filtered = all_objects.filter(score__lt=100)
#2
6
You are saying that The max possible score is 100
. By using score__lte=100
, you are filtering all objects with score
less than or equal to 100 - which is every object in the table by your own definition.
您说最大可能得分为100.通过使用score__lte = 100,您将筛选得分小于或等于100的所有对象 - 这是您自己定义的表中的每个对象。
#1
7
As you said max possible score is 100
so it will always return all objects because lte
means return all objects whose score is either less than or equal to 100
. You might need lt
lookup which means just return those objects whose score is less than 100
:
正如你所说的最大可能得分为100所以它总会返回所有对象,因为lte意味着返回得分小于或等于100的所有对象。你可能需要lt lookup,这意味着只返回那些得分低于100的对象:
filtered = all_objects.filter(score__lt=100)
#2
6
You are saying that The max possible score is 100
. By using score__lte=100
, you are filtering all objects with score
less than or equal to 100 - which is every object in the table by your own definition.
您说最大可能得分为100.通过使用score__lte = 100,您将筛选得分小于或等于100的所有对象 - 这是您自己定义的表中的每个对象。