Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.0.X] Fixed #9252 -- Moved the try/except protecting against incorr…

…ect lookup params to where the error is now raised, and added a test for this case.

Backport of [9245] from trunk.


git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@9246 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 74997f95def1ed655f24bdd526aed1f8912fe646 1 parent d7ebda8
Karen Tracey authored
19  django/contrib/admin/views/main.py
@@ -99,14 +99,7 @@ def get_query_string(self, new_params=None, remove=None):
99 99
     def get_results(self, request):
100 100
         paginator = Paginator(self.query_set, self.list_per_page)
101 101
         # Get the number of objects, with admin filters applied.
102  
-        try:
103  
-            result_count = paginator.count
104  
-        # Naked except! Because we don't have any other way of validating
105  
-        # "params". They might be invalid if the keyword arguments are
106  
-        # incorrect, or if the values are not in the correct type (which would
107  
-        # result in a database error).
108  
-        except:
109  
-            raise IncorrectLookupParameters
  102
+        result_count = paginator.count
110 103
 
111 104
         # Get the total number of objects, with no admin filters applied.
112 105
         # Perform a slight optimization: Check to see whether any filters were
@@ -192,7 +185,15 @@ def get_query_set(self):
192 185
                 lookup_params[key] = value.split(',')
193 186
 
194 187
         # Apply lookup parameters from the query string.
195  
-        qs = qs.filter(**lookup_params)
  188
+        try:
  189
+            qs = qs.filter(**lookup_params)
  190
+        # Naked except! Because we don't have any other way of validating "params".
  191
+        # They might be invalid if the keyword arguments are incorrect, or if the
  192
+        # values are not in the correct type, so we might get FieldError, ValueError,
  193
+        # ValicationError, or ? from a custom field that raises yet something else 
  194
+        # when handed impossible data.
  195
+        except:
  196
+            raise IncorrectLookupParameters
196 197
 
197 198
         # Use select_related() if one of the list_display options is a field
198 199
         # with a relationship.
9  tests/regressiontests/admin_views/tests.py
@@ -160,7 +160,14 @@ def testLimitedFilter(self):
160 160
             '<a href="?color__id__exact=3">Blue</a>' in response.content,
161 161
             "Changelist filter not correctly limited by limit_choices_to."
162 162
         )
163  
-    
  163
+        
  164
+    def testIncorrectLookupParameters(self):
  165
+        """Ensure incorrect lookup parameters are handled gracefully."""
  166
+        response = self.client.get('/test_admin/admin/admin_views/thing/', {'notarealfield': '5'})
  167
+        self.assertRedirects(response, '/test_admin/admin/admin_views/thing/?e=1')        
  168
+        response = self.client.get('/test_admin/admin/admin_views/thing/', {'color__id__exact': 'StringNotInteger!'})
  169
+        self.assertRedirects(response, '/test_admin/admin/admin_views/thing/?e=1')
  170
+            
164 171
 def get_perm(Model, perm):
165 172
     """Return the permission object, for the Model"""
166 173
     ct = ContentType.objects.get_for_model(Model)

0 notes on commit 74997f9

Please sign in to comment.
Something went wrong with that request. Please try again.