Django:按两个值之间的整数过滤对象 [英] Django: Filter objects by integer between two values
问题描述
我正在努力解决迄今为止无法解决的Django过滤问题。我有一个从/到整数的数据库,我需要一个Django过滤器,该过滤器返回给定整数在该范围内的任何对象。
I'm struggling with a Django filtering problem I couldn't solve so far. I have a database with from/to integers, and I need a Django Filter that returns any objects where a given integer is within that range.
我有以下模型(简化):
I have the following model (simplified):
class Dataset(models.Model):
i_begin_int = models.BigIntegerField()
i_end_int = models.BigIntegerField()
例如,我有以下数据:
+----+-------------+-----------+
| id | i_begin_int | i_end_int |
+----+-------------+-----------+
| 1 | 100 | 200 |
+----+-------------+-----------+
| 2 | 150 | 300 |
+----+-------------+-----------+
| 3 | 7000 | 7500 |
+----+-------------+-----------+
所以我现在有一个整数,比方说170。我需要在 i_begin_int
和 i_end_int
。在示例表中,该对象将是ID为1和2的对象。
So now I have an integer, lets say, 170. I need all objects where 170 is between i_begin_int
and i_end_int
. In the example table, that would be objects with id 1 and 2.
是否存在可以用于此目的的Django过滤器?
Is there a Django filter that I could use for this?
推荐答案
尝试一下;
x = 170
Dataset.objects.filter(i_end_int__gte=x,i_begin_int__lte=x)
其中;
gte =大于等于
lte =小于等于
where; gte = greater than equal to lte = less than equal to
这篇关于Django:按两个值之间的整数过滤对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!