mirror of
https://github.com/django/django.git
synced 2025-01-03 06:55:47 +00:00
Fixed #23971 -- Added "Has date"/"No date" choices for DateFieldListFilter.
This commit is contained in:
parent
67907ed845
commit
468d8211df
1
AUTHORS
1
AUTHORS
@ -638,6 +638,7 @@ answer newbie questions, and generally made Django that much better:
|
|||||||
ryankanno
|
ryankanno
|
||||||
Ryan Kelly <ryan@rfk.id.au>
|
Ryan Kelly <ryan@rfk.id.au>
|
||||||
Ryan Niemeyer <https://profiles.google.com/ryan.niemeyer/about>
|
Ryan Niemeyer <https://profiles.google.com/ryan.niemeyer/about>
|
||||||
|
Ryno Mathee <rmathee@gmail.com>
|
||||||
Sam Newman <http://www.magpiebrain.com/>
|
Sam Newman <http://www.magpiebrain.com/>
|
||||||
Sander Dijkhuis <sander.dijkhuis@gmail.com>
|
Sander Dijkhuis <sander.dijkhuis@gmail.com>
|
||||||
Sarthak Mehrish <sarthakmeh03@gmail.com>
|
Sarthak Mehrish <sarthakmeh03@gmail.com>
|
||||||
|
@ -331,11 +331,20 @@ class DateFieldListFilter(FieldListFilter):
|
|||||||
self.lookup_kwarg_until: str(next_year),
|
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__(
|
super(DateFieldListFilter, self).__init__(
|
||||||
field, request, params, model, model_admin, field_path)
|
field, request, params, model, model_admin, field_path)
|
||||||
|
|
||||||
def expected_parameters(self):
|
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):
|
def choices(self, changelist):
|
||||||
for title, param_dict in self.links:
|
for title, param_dict in self.links:
|
||||||
|
@ -59,6 +59,9 @@ Minor features
|
|||||||
* Selected objects for fields in ``ModelAdmin.raw_id_fields`` now have a link
|
* Selected objects for fields in ``ModelAdmin.raw_id_fields`` now have a link
|
||||||
to object's change form.
|
to object's change form.
|
||||||
|
|
||||||
|
* Added "No date" and "Has date" choices for ``DateFieldListFilter`` if the
|
||||||
|
field is nullable.
|
||||||
|
|
||||||
:mod:`django.contrib.admindocs`
|
:mod:`django.contrib.admindocs`
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
@ -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(
|
@unittest.skipIf(
|
||||||
sys.platform.startswith('win'),
|
sys.platform.startswith('win'),
|
||||||
"Windows doesn't support setting a timezone that differs from the "
|
"Windows doesn't support setting a timezone that differs from the "
|
||||||
|
Loading…
Reference in New Issue
Block a user