From 069319396f0e9fb10254e1bcf578336392c9cffb Mon Sep 17 00:00:00 2001 From: Vincenzo Pandolfo Date: Wed, 30 Mar 2016 16:05:31 +0100 Subject: Fixed #26277 -- Added support for null values in ChoicesFieldListFilter. --- tests/admin_filters/models.py | 7 +++++++ tests/admin_filters/tests.py | 16 ++++++++++++++++ 2 files changed, 23 insertions(+) (limited to 'tests') diff --git a/tests/admin_filters/models.py b/tests/admin_filters/models.py index db37958882..8860201d35 100644 --- a/tests/admin_filters/models.py +++ b/tests/admin_filters/models.py @@ -75,5 +75,12 @@ class Bookmark(models.Model): url = models.URLField() tags = GenericRelation(TaggedItem) + CHOICES = [ + ('a', 'A'), + (None, 'None'), + ('', '-'), + ] + none_or_null = models.CharField(max_length=20, choices=CHOICES, blank=True, null=True) + def __str__(self): return self.url diff --git a/tests/admin_filters/tests.py b/tests/admin_filters/tests.py index 3d2f7ab9c6..ba6934f4ed 100644 --- a/tests/admin_filters/tests.py +++ b/tests/admin_filters/tests.py @@ -302,6 +302,22 @@ class ListFiltersTests(TestCase): modeladmin.list_max_show_all, modeladmin.list_editable, modeladmin, ) + def test_choicesfieldlistfilter_has_none_choice(self): + """ + The last choice is for the None value. + """ + class BookmarkChoicesAdmin(ModelAdmin): + list_display = ['none_or_null'] + list_filter = ['none_or_null'] + + modeladmin = BookmarkChoicesAdmin(Bookmark, site) + request = self.request_factory.get('/', {}) + changelist = self.get_changelist(request, Bookmark, modeladmin) + filterspec = changelist.get_filters(request)[0][0] + choices = list(filterspec.choices(changelist)) + self.assertEqual(choices[-1]['display'], 'None') + self.assertEqual(choices[-1]['query_string'], '?none_or_null__isnull=True') + def test_datefieldlistfilter(self): modeladmin = BookAdmin(Book, site) -- cgit v1.2.1