TAGS :Viewed: 1 - Published at: a few seconds ago

[ how to filter given two datefields if they coincide with a from and to date in a model django ]

I have a 2 dates from 2 datepickers date1 = '2013-01-22' date2 = '2013-01-28' and I have a model like this

class SampleModel(models.Model):
    item = models.CharField()
    from_date = models.DateField()
    to_date = models.DateField()

What I am tasked to do is to know if some of the dates between date1 and date2 is between those from_date and to_date (same item)

so for example if i have a from_date of '2013-01-24' and to_date '2013-01-27', some of the dates from input date1 and date2 (which are the days 24 25 26 27) are included and prompts true.

It's like an intersection. Any queries?

a gte on from_date and lte on to_date doesn't work since date1 is less than from date and date2 is greater than to date

Answer 1


That's a math question. This query should do the trick:

date1 <= to_date and date2 >= from_date

Edit

The following django statement should give you the desired result:

SampleModel.objects.filter(to_date__gte=date1, from_date__lte=date2)