diff --git a/daterangefilter/filters.py b/daterangefilter/filters.py index 385c0b5..192b017 100644 --- a/daterangefilter/filters.py +++ b/daterangefilter/filters.py @@ -1,4 +1,8 @@ +import datetime + +from django.db import models from django.contrib import admin +from django.utils import timezone from django.utils.translation import gettext_lazy as _ from django.conf import settings @@ -17,6 +21,16 @@ def __init__(self, field, request, params, model, model_admin, field_path): params.pop(self.lookup_kwarg_lte) if self.lookup_gte and self.lookup_lte: self.lookup_val = '{} - {}'.format(self.lookup_gte.replace('-','.'), self.lookup_lte.replace('-','.')) + # if we are filtering DateTimeField we should add one day to final date + if isinstance(model._meta.get_field(field_path), models.DateTimeField): + gte_date = datetime.datetime.strptime(self.lookup_gte, '%Y-%m-%d') + lte_date = datetime.datetime.strptime(self.lookup_lte, '%Y-%m-%d') + lte_date = lte_date + datetime.timedelta(seconds=3600*24-1) + if settings.USE_TZ: + gte_date = timezone.make_aware(gte_date, timezone.get_current_timezone()) + lte_date = timezone.make_aware(lte_date, timezone.get_current_timezone()) + params[self.lookup_kwarg_gte] = gte_date.strftime('%Y-%m-%d %H:%M:%S%z') + params[self.lookup_kwarg_lte] = lte_date.strftime('%Y-%m-%d %H:%M:%S%z') else: self.lookup_val = '' super(DateRangeFilter, self).__init__(field, request, params, model, model_admin, field_path)