简体   繁体   中英

Django Queryset find data between date

I don't know what title should be, I just got stuck and need to ask.

I have a model called shift

and imagine the db_table like this:

#table shift
+---------------+---------------+---------------+---------------+------------+------------+
| start         | end           | off_start     | off_end       | time       | user_id    |
+---------------+---------------+---------------+---------------+------------+------------+
| 2018-01-01    | 2018-01-05    | 2018-01-06    | 2018-01-07    | 07:00      | 1          |
| 2018-01-08    | 2018-01-14    | 2018-01-15    | Null          | 12:00      | 1          |
| 2018-01-16    | 2018-01-20    | 2018-01-21    | 2018-01-22    | 18:00      | 1          |
| 2018-01-23    | 2018-01-27    | 2018-01-28    | 2018-01-31    | 24:00      | 1          |
| ....          | ....          | ....          | ....          | ....       | ....       |
+---------------+---------------+---------------+---------------+------------+------------+

if I use queryset with filter like start=2018-01-01 result will 07:00

but how to get result 12:00 if I Input 2018-01-10 ?...

thank you!

问题不是很清楚,但也许您正在start__lte=2018-01-10, end__gte=2018-01-10类似start__lte=2018-01-10, end__gte=2018-01-10东西?

Well usually you can do that with an interval check: the start should be less than or equal to the date you want to query, and the end should be greater than or equal than the date you want to query.

So you can obtain the Shift instances where they have a time range that includes the given range with:

from datetime import date

t0 = date(2018, 1, 10)

Shift.objects.filter()

In case the start and/or end dates are non inclusive, remove the corresponding e suffix(es).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM