How to do less than or equal to and greater than equal to in django filter? Like , I want to get value around :- 10<=val<=50
in django view.
For this I used some query in sql like this :-
如何在django过滤器中做小于或等于和大于等于?就像,我想获得价值: - django视图中的10 <= val <= 50。为此,我在sql中使用了一些查询:
select count(*) from table_name where gender='MALE' and age<=50 and age>=10;
I tried something like this in django view :-
我在django视图中尝试过类似的东西: -
tablename.objects.filter(Q(gender='MALE'),Q(age__lte=50) & Q(age__gte=10)).count()
But I got different values. In sql I got 65 and in django I got 29. sql answer is correct. Please help me to do comparison in django view.
但我得到了不同的价值观。在sql我得到65和django我得到29. sql答案是正确的。请帮我在django视图中做比较。
2 个解决方案
#1
12
Why don't you use the _range function?
你为什么不使用_range函数?
filter(gender='MALE', age__range=(10, 50))
过滤器(gender ='MALE',age__range =(10,50))
See here: https://docs.djangoproject.com/en/1.7/ref/models/querysets/#range
请参见:https://docs.djangoproject.com/en/1.7/ref/models/querysets/#range
#2
10
If you really want to use >=
and <=
yo could write:
如果你真的想使用> =和<=你可以写:
Modelname.objects.filter(gender='MALE', age__gte = 10, age__lte = 50).count()
#1
12
Why don't you use the _range function?
你为什么不使用_range函数?
filter(gender='MALE', age__range=(10, 50))
过滤器(gender ='MALE',age__range =(10,50))
See here: https://docs.djangoproject.com/en/1.7/ref/models/querysets/#range
请参见:https://docs.djangoproject.com/en/1.7/ref/models/querysets/#range
#2
10
If you really want to use >=
and <=
yo could write:
如果你真的想使用> =和<=你可以写:
Modelname.objects.filter(gender='MALE', age__gte = 10, age__lte = 50).count()