Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

python - Django: Filter objects by integer between two values

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()

So for example, I have the following data:

+----+-------------+-----------+
| id | i_begin_int | i_end_int |
+----+-------------+-----------+
|  1 |         100 |       200 |
+----+-------------+-----------+
|  2 |         150 |       300 |
+----+-------------+-----------+
|  3 |        7000 |      7500 |
+----+-------------+-----------+

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.

Is there a Django filter that I could use for this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this;

x = 170
Dataset.objects.filter(i_end_int__gte=x,i_begin_int__lte=x)

where; gte = greater than equal to lte = less than equal to


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...