Fixed #23971 -- Added "Has date"/"No date" choices for DateFieldListFilter.

This commit is contained in:
rynomster 2016-01-14 10:26:44 +02:00 committed by Tim Graham
parent 67907ed845
commit 468d8211df
4 changed files with 45 additions and 1 deletions

View File

@ -638,6 +638,7 @@ answer newbie questions, and generally made Django that much better:
ryankanno
Ryan Kelly <ryan@rfk.id.au>
Ryan Niemeyer <https://profiles.google.com/ryan.niemeyer/about>
Ryno Mathee <rmathee@gmail.com>
Sam Newman <http://www.magpiebrain.com/>
Sander Dijkhuis <sander.dijkhuis@gmail.com>
Sarthak Mehrish <sarthakmeh03@gmail.com>

View File

@ -331,11 +331,20 @@ class DateFieldListFilter(FieldListFilter):
self.lookup_kwarg_until: str(next_year),
}),
)
if field.null:
self.lookup_kwarg_isnull = '%s__isnull' % field_path
self.links += (
(_('No date'), {self.field_generic + 'isnull': 'True'}),
(_('Has date'), {self.field_generic + 'isnull': 'False'}),
)
super(DateFieldListFilter, self).__init__(
field, request, params, model, model_admin, field_path)
def expected_parameters(self):
return [self.lookup_kwarg_since, self.lookup_kwarg_until]
params = [self.lookup_kwarg_since, self.lookup_kwarg_until]
if self.field.null:
params.append(self.lookup_kwarg_isnull)
return params
def choices(self, changelist):
for title, param_dict in self.links:

View File

@ -59,6 +59,9 @@ Minor features
* Selected objects for fields in ``ModelAdmin.raw_id_fields`` now have a link
to object's change form.
* Added "No date" and "Has date" choices for ``DateFieldListFilter`` if the
field is nullable.
:mod:`django.contrib.admindocs`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -402,6 +402,37 @@ class ListFiltersTests(TestCase):
)
)
# Null/not null queries
request = self.request_factory.get('/', {'date_registered__isnull': 'True'})
changelist = self.get_changelist(request, Book, modeladmin)
# Make sure the correct queryset is returned
queryset = changelist.get_queryset(request)
self.assertEqual(queryset.count(), 1)
self.assertEqual(queryset[0], self.bio_book)
# Make sure the correct choice is selected
filterspec = changelist.get_filters(request)[0][4]
self.assertEqual(force_text(filterspec.title), 'date registered')
choice = select_by(filterspec.choices(changelist), 'display', 'No date')
self.assertEqual(choice['selected'], True)
self.assertEqual(choice['query_string'], '?date_registered__isnull=True')
request = self.request_factory.get('/', {'date_registered__isnull': 'False'})
changelist = self.get_changelist(request, Book, modeladmin)
# Make sure the correct queryset is returned
queryset = changelist.get_queryset(request)
self.assertEqual(queryset.count(), 3)
self.assertEqual(list(queryset), [self.gipsy_book, self.django_book, self.djangonaut_book])
# Make sure the correct choice is selected
filterspec = changelist.get_filters(request)[0][4]
self.assertEqual(force_text(filterspec.title), 'date registered')
choice = select_by(filterspec.choices(changelist), 'display', 'Has date')
self.assertEqual(choice['selected'], True)
self.assertEqual(choice['query_string'], '?date_registered__isnull=False')
@unittest.skipIf(
sys.platform.startswith('win'),
"Windows doesn't support setting a timezone that differs from the "