简体   繁体   中英

Django queryset get all data that pass a selected date

I created an object from a model call Project which look like this :

[
{
    "name": "project A"
    "start_date": 2019-01-01
    "end_date": 2019-02-15
},
{
    "name": "project B"
    "start_date": 2019-01-15
    "end_date": 2019-02-01
},
{
    "name": "project C"
    "start_date": 2019-02-27
    "end_date": 2019-03-12
},
]

if I have a date range selector and I select the date from 2019-01-07 until 2019-02-10 , and my query look like this

model_object = Project.objects.all().filter(start_date__gte=(2019-01-07), end_date__lte=(2019-02-10))

The result of model_object queryset will be :

{
    "name": "project B"
    "start_date": 2019-01-15
    "end_date": 2019-02-01
},

What I wanted now is how can I filter the query in such way where I get all the object that is ongoing in between the date 2019-01-07 until 2019-02-10 which mean the result should also include project A because its still ongoing from 2019-01-01 until 2019-02-15 even though it started before 2019-01-07

Any help is much appreciated thanks.

You can try like this:

import dateutil.parser

model_object = Project.objects.filter(start_date__lte=dateutil.parser.parse('2019-01-07'),  end_date__gte=dateutil.parser.parse('2019-02-10'))

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