Author: julien
Date: 2012-03-17 14:45:36 -0700 (Sat, 17 Mar 2012)
New Revision: 17763
Modified:
django/trunk/django/contrib/admin/options.py
django/trunk/django/contrib/admin/views/main.py
django/trunk/tests/regressiontests/admin_filters/tests.py
Log:
Fixed #17828 -- Ensured that when a list filter's `queryset()` method fails, it
does so loudly instead of getting swallowed by a `IncorrectLookupParameters`
exception. This also properly fixes #16705, which hadn't been addressed
correctly in [16705].
Modified: django/trunk/django/contrib/admin/options.py
===================================================================
--- django/trunk/django/contrib/admin/options.py 2012-03-17 20:13:06 UTC
(rev 17762)
+++ django/trunk/django/contrib/admin/options.py 2012-03-17 21:45:36 UTC
(rev 17763)
@@ -247,7 +247,12 @@
# the pk attribute name.
pk_attr_name = None
for part in parts[:-1]:
- field, _, _, _ = model._meta.get_field_by_name(part)
+ try:
+ field, _, _, _ = model._meta.get_field_by_name(part)
+ except FieldDoesNotExist:
+ # Lookups on non-existants fields are ok, since they're ignored
+ # later.
+ return True
if hasattr(field, 'rel'):
model = field.rel.to
pk_attr_name = model._meta.pk.name
@@ -259,17 +264,10 @@
if pk_attr_name and len(parts) > 1 and parts[-1] == pk_attr_name:
parts.pop()
- try:
- self.model._meta.get_field_by_name(parts[0])
- except FieldDoesNotExist:
- # Lookups on non-existants fields are ok, since they're ignored
- # later.
+ if len(parts) == 1:
return True
- else:
- if len(parts) == 1:
- return True
- clean_lookup = LOOKUP_SEP.join(parts)
- return clean_lookup in self.list_filter or clean_lookup ==
self.date_hierarchy
+ clean_lookup = LOOKUP_SEP.join(parts)
+ return clean_lookup in self.list_filter or clean_lookup ==
self.date_hierarchy
def has_add_permission(self, request):
"""
Modified: django/trunk/django/contrib/admin/views/main.py
===================================================================
--- django/trunk/django/contrib/admin/views/main.py 2012-03-17 20:13:06 UTC
(rev 17762)
+++ django/trunk/django/contrib/admin/views/main.py 2012-03-17 21:45:36 UTC
(rev 17763)
@@ -3,6 +3,7 @@
from django.core.exceptions import SuspiciousOperation, ImproperlyConfigured
from django.core.paginator import InvalidPage
from django.db import models
+from django.db.models.fields import FieldDoesNotExist
from django.utils.datastructures import SortedDict
from django.utils.encoding import force_unicode, smart_str
from django.utils.translation import ugettext, ugettext_lazy
@@ -130,14 +131,17 @@
# have been removed from lookup_params, which now only contains other
# parameters passed via the query string. We now loop through the
# remaining parameters both to ensure that all the parameters are valid
- # fields and to determine if at least one of them needs distinct().
- for key, value in lookup_params.items():
- lookup_params[key] = prepare_lookup_value(key, value)
- use_distinct = (use_distinct or
- lookup_needs_distinct(self.lookup_opts, key))
+ # fields and to determine if at least one of them needs distinct(). If
+ # the lookup parameters aren't real fields, then bail out.
+ try:
+ for key, value in lookup_params.items():
+ lookup_params[key] = prepare_lookup_value(key, value)
+ use_distinct = (use_distinct or
+ lookup_needs_distinct(self.lookup_opts, key))
+ return filter_specs, bool(filter_specs), lookup_params,
use_distinct
+ except FieldDoesNotExist, e:
+ raise IncorrectLookupParameters(e)
- return filter_specs, bool(filter_specs), lookup_params, use_distinct
-
def get_query_string(self, new_params=None, remove=None):
if new_params is None: new_params = {}
if remove is None: remove = []
@@ -292,18 +296,18 @@
return ordering_fields
def get_query_set(self, request):
- try:
- # First, we collect all the declared list filters.
- (self.filter_specs, self.has_filters, remaining_lookup_params,
- use_distinct) = self.get_filters(request)
+ # First, we collect all the declared list filters.
+ (self.filter_specs, self.has_filters, remaining_lookup_params,
+ use_distinct) = self.get_filters(request)
- # Then, we let every list filter modify the qs to its liking.
- qs = self.root_query_set
- for filter_spec in self.filter_specs:
- new_qs = filter_spec.queryset(request, qs)
- if new_qs is not None:
- qs = new_qs
+ # Then, we let every list filter modify the queryset to its liking.
+ qs = self.root_query_set
+ for filter_spec in self.filter_specs:
+ new_qs = filter_spec.queryset(request, qs)
+ if new_qs is not None:
+ qs = new_qs
+ try:
# Finally, we apply the remaining lookup parameters from the query
# string (i.e. those that haven't already been processed by the
# filters).
@@ -317,8 +321,7 @@
# have any other way of validating lookup parameters. They might be
# invalid if the keyword arguments are incorrect, or if the values
# are not in the correct type, so we might get FieldError,
- # ValueError, ValidationError, or ? from a custom field that raises
- # yet something else when handed impossible data.
+ # ValueError, ValidationError, or ?.
raise IncorrectLookupParameters(e)
# Use select_related() if one of the list_display options is a field
Modified: django/trunk/tests/regressiontests/admin_filters/tests.py
===================================================================
--- django/trunk/tests/regressiontests/admin_filters/tests.py 2012-03-17
20:13:06 UTC (rev 17762)
+++ django/trunk/tests/regressiontests/admin_filters/tests.py 2012-03-17
21:45:36 UTC (rev 17763)
@@ -56,7 +56,7 @@
class
DecadeListFilterWithFailingQueryset(DecadeListFilterWithTitleAndParameter):
def queryset(self, request, queryset):
- raise Exception
+ raise 1/0
class
DecadeListFilterWithQuerysetBasedLookups(DecadeListFilterWithTitleAndParameter):
@@ -77,6 +77,7 @@
title = 'publication decade'
parameter_name = 'decade__isnull' # Ends with '__isnull"
+
class CustomUserAdmin(UserAdmin):
list_filter = ('books_authored', 'books_contributed')
@@ -112,6 +113,8 @@
class DecadeFilterBookAdminParameterEndsWith__Isnull(ModelAdmin):
list_filter = (DecadeListFilterParameterEndsWith__Isnull,)
+
+
class ListFiltersTests(TestCase):
def setUp(self):
@@ -542,14 +545,13 @@
def test_filter_with_failing_queryset(self):
"""
- Ensure that a filter's failing queryset is interpreted as if incorrect
- lookup parameters were passed (therefore causing a 302 redirection to
- the changelist).
- Refs #16716, #16714.
+ Ensure that when a filter's queryset method fails, it fails loudly and
+ the corresponding exception doesn't get swallowed.
+ Refs #17828.
"""
modeladmin = DecadeFilterBookAdminWithFailingQueryset(Book, site)
request = self.request_factory.get('/', {})
- self.assertRaises(IncorrectLookupParameters, self.get_changelist,
request, Book, modeladmin)
+ self.assertRaises(ZeroDivisionError, self.get_changelist, request,
Book, modeladmin)
def test_simplelistfilter_with_queryset_based_lookups(self):
modeladmin = DecadeFilterBookAdminWithQuerysetBasedLookups(Book, site)
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en.