0

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

Leonid
  • 340
  • 1
  • 3
  • 16
  • possible duplicate of [How do you select between two dates with Django](http://stackoverflow.com/questions/3963201/how-do-you-select-between-two-dates-with-django) – Jon Clements Jan 15 '13 at 17:44
  • I think not. it's like a range from a range. __range is for 1 field in the model only. i'm comparing two date inputs here from two datefields in the model. date1 and date2 is a date range. that means is is from january 22 upto 28. – Leonid Jan 15 '13 at 17:49
  • I misread that - my apologies :( – Jon Clements Jan 15 '13 at 17:51
  • lol sorry for that. i should have used "between" tho for clarity – Leonid Jan 15 '13 at 17:53

1 Answers1

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)
ilmiacs
  • 2,566
  • 15
  • 20
  • but how can I query an "and"? – Leonid Jan 15 '13 at 18:17
  • 1
    According to the django docs, you also can filter for more than one constraint: `SampleModel.objects.filter(to_date__gte=date1, from_date__lte<=date2)`. This is more efficient than filtering twice. – ilmiacs Jan 15 '13 at 18:21