Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #21186: Fixed regression when using date fields in the admin's …

…list_filter.

Thanks to onlygoldi2201 for the report and to ramiro and apollo13
for the reviews.
  • Loading branch information...
commit 8f51ba669aba94eea684ea3f3429fd8e39e70679 1 parent 8a4f5de
@bmispelon bmispelon authored
Showing with 16 additions and 6 deletions.
  1. +7 −2 django/contrib/admin/filters.py
  2. +9 −4 tests/admin_filters/tests.py
View
9 django/contrib/admin/filters.py
@@ -303,6 +303,11 @@ def __init__(self, field, request, params, model, model_admin, field_path):
else: # field is a models.DateField
today = now.date()
tomorrow = today + datetime.timedelta(days=1)
+ if today.month == 12:
+ next_month = today.replace(year=today.year + 1, month=1, day=1)
+ else:
+ next_month = today.replace(month=today.month + 1, day=1)
+ next_year = today.replace(year=today.year + 1, month=1, day=1)
self.lookup_kwarg_since = '%s__gte' % field_path
self.lookup_kwarg_until = '%s__lt' % field_path
@@ -318,11 +323,11 @@ def __init__(self, field, request, params, model, model_admin, field_path):
}),
(_('This month'), {
self.lookup_kwarg_since: str(today.replace(day=1)),
- self.lookup_kwarg_until: str(tomorrow),
+ self.lookup_kwarg_until: str(next_month),
}),
(_('This year'), {
self.lookup_kwarg_since: str(today.replace(month=1, day=1)),
- self.lookup_kwarg_until: str(tomorrow),
+ self.lookup_kwarg_until: str(next_year),
}),
)
super(DateFieldListFilter, self).__init__(
View
13 tests/admin_filters/tests.py
@@ -143,6 +143,11 @@ def setUp(self):
self.today = datetime.date.today()
self.tomorrow = self.today + datetime.timedelta(days=1)
self.one_week_ago = self.today - datetime.timedelta(days=7)
+ if self.today.month == 12:
+ self.next_month = self.today.replace(year=self.today.year + 1, month=1, day=1)
+ else:
+ self.next_month = self.today.replace(month=self.today.month + 1, day=1)
+ self.next_year = self.today.replace(year=self.today.year + 1, month=1, day=1)
self.request_factory = RequestFactory()
@@ -196,7 +201,7 @@ def test_datefieldlistfilter(self):
% (self.today, self.tomorrow))
request = self.request_factory.get('/', {'date_registered__gte': self.today.replace(day=1),
- 'date_registered__lt': self.tomorrow})
+ 'date_registered__lt': self.next_month})
changelist = self.get_changelist(request, Book, modeladmin)
# Make sure the correct queryset is returned
@@ -214,10 +219,10 @@ def test_datefieldlistfilter(self):
self.assertEqual(choice['selected'], True)
self.assertEqual(choice['query_string'], '?date_registered__gte=%s'
'&date_registered__lt=%s'
- % (self.today.replace(day=1), self.tomorrow))
+ % (self.today.replace(day=1), self.next_month))
request = self.request_factory.get('/', {'date_registered__gte': self.today.replace(month=1, day=1),
- 'date_registered__lt': self.tomorrow})
+ 'date_registered__lt': self.next_year})
changelist = self.get_changelist(request, Book, modeladmin)
# Make sure the correct queryset is returned
@@ -235,7 +240,7 @@ def test_datefieldlistfilter(self):
self.assertEqual(choice['selected'], True)
self.assertEqual(choice['query_string'], '?date_registered__gte=%s'
'&date_registered__lt=%s'
- % (self.today.replace(month=1, day=1), self.tomorrow))
+ % (self.today.replace(month=1, day=1), self.next_year))
request = self.request_factory.get('/', {'date_registered__gte': str(self.one_week_ago),
'date_registered__lt': str(self.tomorrow)})
Please sign in to comment.
Something went wrong with that request. Please try again.