Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #357 from tswicegood/fix-list-attr

Removed admin's swallowing of AttributeError (#16655, #18593, #18747)
  • Loading branch information...
commit 617d077f1f5be6cfb67a1f4f9ab0334f3c793e4c 2 parents d823bb7 + ccd1bb0
Aymeric Augustin authored
2  django/contrib/admin/templatetags/admin_list.py
@@ -182,7 +182,7 @@ def items_for_result(cl, result, form):
182 182
         row_class = ''
183 183
         try:
184 184
             f, attr, value = lookup_field(field_name, result, cl.model_admin)
185  
-        except (AttributeError, ObjectDoesNotExist):
  185
+        except ObjectDoesNotExist:
186 186
             result_repr = EMPTY_CHANGELIST_VALUE
187 187
         else:
188 188
             if f is None:
16  tests/regressiontests/admin_views/admin.py
@@ -27,11 +27,14 @@
27 27
     Album, Question, Answer, ComplexSortedPerson, PrePopulatedPostLargeSlug,
28 28
     AdminOrderedField, AdminOrderedModelMethod, AdminOrderedAdminMethod,
29 29
     AdminOrderedCallable, Report, Color2, UnorderedObject, MainPrepopulated,
30  
-    RelatedPrepopulated, UndeletableObject)
  30
+    RelatedPrepopulated, UndeletableObject, Simple)
31 31
 
32 32
 
33 33
 def callable_year(dt_value):
34  
-    return dt_value.year
  34
+    try:
  35
+        return dt_value.year
  36
+    except AttributeError:
  37
+        return None
35 38
 callable_year.admin_order_field = 'date'
36 39
 
37 40
 
@@ -575,6 +578,14 @@ def change_view(self, *args, **kwargs):
575 578
         return super(UndeletableObjectAdmin, self).change_view(*args, **kwargs)
576 579
 
577 580
 
  581
+def callable_on_unknown(obj):
  582
+    return obj.unknown
  583
+
  584
+
  585
+class AttributeErrorRaisingAdmin(admin.ModelAdmin):
  586
+    list_display = [callable_on_unknown, ]
  587
+
  588
+
578 589
 site = admin.AdminSite(name="admin")
579 590
 site.register(Article, ArticleAdmin)
580 591
 site.register(CustomArticle, CustomArticleAdmin)
@@ -648,6 +659,7 @@ def change_view(self, *args, **kwargs):
648 659
 site.register(AdminOrderedAdminMethod, AdminOrderedAdminMethodAdmin)
649 660
 site.register(AdminOrderedCallable, AdminOrderedCallableAdmin)
650 661
 site.register(Color2, CustomTemplateFilterColorAdmin)
  662
+site.register(Simple, AttributeErrorRaisingAdmin)
651 663
 
652 664
 # Register core models we need in our tests
653 665
 from django.contrib.auth.models import User, Group
1  tests/regressiontests/admin_views/customadmin.py
@@ -49,3 +49,4 @@ def queryset(self, request):
49 49
 site.register(models.ChapterXtra1, base_admin.ChapterXtra1Admin)
50 50
 site.register(User, UserLimitedAdmin)
51 51
 site.register(models.UndeletableObject, base_admin.UndeletableObjectAdmin)
  52
+site.register(models.Simple, base_admin.AttributeErrorRaisingAdmin)
6  tests/regressiontests/admin_views/models.py
@@ -649,3 +649,9 @@ class UndeletableObject(models.Model):
649 649
     Refs #10057.
650 650
     """
651 651
     name = models.CharField(max_length=255)
  652
+
  653
+
  654
+class Simple(models.Model):
  655
+    """
  656
+    Simple model with nothing on it for use in testing
  657
+    """
16  tests/regressiontests/admin_views/tests.py
@@ -46,7 +46,7 @@
46 46
     OtherStory, ComplexSortedPerson, Parent, Child, AdminOrderedField,
47 47
     AdminOrderedModelMethod, AdminOrderedAdminMethod, AdminOrderedCallable,
48 48
     Report, MainPrepopulated, RelatedPrepopulated, UnorderedObject,
49  
-    UndeletableObject)
  49
+    Simple, UndeletableObject)
50 50
 
51 51
 
52 52
 ERROR_MESSAGE = "Please enter the correct username and password \
@@ -578,6 +578,20 @@ def test_change_view_with_show_delete_extra_context(self):
578 578
                                    (self.urlbit, instance.pk))
579 579
         self.assertNotContains(response, 'deletelink')
580 580
 
  581
+    def test_allows_attributeerror_to_bubble_up(self):
  582
+        """
  583
+        Ensure that AttributeErrors are allowed to bubble when raised inside
  584
+        a change list view.
  585
+
  586
+        Requires a model to be created so there's something to be displayed
  587
+
  588
+        Refs: #16655, #18593, and #18747
  589
+        """
  590
+        Simple.objects.create()
  591
+        with self.assertRaises(AttributeError):
  592
+            self.client.get('/test_admin/%s/admin_views/simple/' % self.urlbit)
  593
+
  594
+
581 595
 @override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
582 596
 class AdminViewFormUrlTest(TestCase):
583 597
     urls = "regressiontests.admin_views.urls"

0 notes on commit 617d077

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