Skip to content

Commit

Permalink
Fix filtering by DateTimeField
Browse files Browse the repository at this point in the history
  • Loading branch information
andreynovikov committed Dec 17, 2018
1 parent f535f68 commit 8477844
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions daterangefilter/filters.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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)
Expand Down

0 comments on commit 8477844

Please sign in to comment.