1

I'm trying to filter the query set on between two dates. here is my views.py

week = (startDate, endDate)
Student.objects.filter(created__range=week)
  • startDate and endDate is date objects

  • created is my datetime field

but I want filter on only date

i'd like to match the month, day, year exactly, not the time.

expected output is filter queryset between startDate and endDate

NIKHIL RANE
  • 4,012
  • 2
  • 22
  • 45
  • duplicate: http://stackoverflow.com/questions/4668619/django-database-query-how-to-filter-objects-by-date-range – warath-coder Feb 10 '15 at 12:31
  • @warath-coder Nope. Objects in the range are dates but search field is the datetime. So simple `__range` will cut all data at `endDate` (`2015-02-10` is less than `2015-02-10 00:00:01`) – catavaran Feb 10 '15 at 12:38
  • if you go to that link, all the possible solutions are there, so yes this is still a duplicate. – warath-coder Feb 10 '15 at 13:08

1 Answers1

4

Replace the __range lookup with __gte/__lt combination.

Student.objects.filter(created__gte=startDate,
                       created__lt=endDate + datetime.timedelta(days=1))
catavaran
  • 44,703
  • 8
  • 98
  • 85