Fixed #28202 -- Fixed FieldListFilter.get_queryset() crash on invalid input.

This commit is contained in:
Paulo 2017-06-03 18:13:38 -04:00 committed by Tim Graham
parent 7f238097c0
commit 4ad2f86284
4 changed files with 15 additions and 2 deletions

View File

@ -132,7 +132,9 @@ class FieldListFilter(ListFilter):
def queryset(self, request, queryset): def queryset(self, request, queryset):
try: try:
return queryset.filter(**self.used_parameters) return queryset.filter(**self.used_parameters)
except ValidationError as e: except (ValueError, ValidationError) as e:
# Fields may raise a ValueError or ValidationError when converting
# the parameters to the correct type.
raise IncorrectLookupParameters(e) raise IncorrectLookupParameters(e)
@classmethod @classmethod

View File

@ -18,3 +18,6 @@ Bugfixes
* Fixed an incorrect ``DisallowedModelAdminLookup`` exception when using * Fixed an incorrect ``DisallowedModelAdminLookup`` exception when using
a nested reverse relation in ``list_filter`` (:ticket:`28262`). a nested reverse relation in ``list_filter`` (:ticket:`28262`).
* Fixed admin's ``FieldListFilter.get_queryset()`` crash on invalid input
(:ticket:`28202`).

View File

@ -6,6 +6,7 @@ from django.contrib.admin import (
AllValuesFieldListFilter, BooleanFieldListFilter, ModelAdmin, AllValuesFieldListFilter, BooleanFieldListFilter, ModelAdmin,
RelatedOnlyFieldListFilter, SimpleListFilter, site, RelatedOnlyFieldListFilter, SimpleListFilter, site,
) )
from django.contrib.admin.options import IncorrectLookupParameters
from django.contrib.admin.views.main import ChangeList from django.contrib.admin.views.main import ChangeList
from django.contrib.auth.admin import UserAdmin from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User from django.contrib.auth.models import User
@ -763,6 +764,13 @@ class ListFiltersTests(TestCase):
queryset = changelist.get_queryset(request) queryset = changelist.get_queryset(request)
self.assertEqual(list(queryset), [self.bio_book, self.djangonaut_book]) self.assertEqual(list(queryset), [self.bio_book, self.djangonaut_book])
def test_fieldlistfilter_invalid_lookup_parameters(self):
"""Filtering by an invalid value."""
modeladmin = BookAdmin(Book, site)
request = self.request_factory.get('/', {'author__id__exact': 'StringNotInteger!'})
with self.assertRaises(IncorrectLookupParameters):
self.get_changelist(request, Book, modeladmin)
def test_simplelistfilter(self): def test_simplelistfilter(self):
modeladmin = DecadeFilterBookAdmin(Book, site) modeladmin = DecadeFilterBookAdmin(Book, site)

View File

@ -165,7 +165,7 @@ class CustomArticleAdmin(admin.ModelAdmin):
class ThingAdmin(admin.ModelAdmin): class ThingAdmin(admin.ModelAdmin):
list_filter = ('color__warm', 'color__value', 'pub_date',) list_filter = ('color', 'color__warm', 'color__value', 'pub_date')
class InquisitionAdmin(admin.ModelAdmin): class InquisitionAdmin(admin.ModelAdmin):