Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #17911 -- Ensure that admin readonly fields' display values are…

… shown in change forms when the raw value is None.
  • Loading branch information...
commit 29d59a879ea5b116cc31a2fd91be1f7562e487c2 1 parent 05dbc07
Edward Tjörnhammar authored November 25, 2012 jphalip committed November 25, 2012
4  django/contrib/admin/helpers.py
@@ -186,9 +186,7 @@ def contents(self):
186 186
                     if getattr(attr, "allow_tags", False):
187 187
                         result_repr = mark_safe(result_repr)
188 188
             else:
189  
-                if value is None:
190  
-                    result_repr = EMPTY_CHANGELIST_VALUE
191  
-                elif isinstance(f.rel, ManyToManyRel):
  189
+                if isinstance(f.rel, ManyToManyRel) and value is not None:
192 190
                     result_repr = ", ".join(map(six.text_type, value.all()))
193 191
                 else:
194 192
                     result_repr = display_for_field(value, f)
9  tests/regressiontests/admin_views/admin.py
@@ -27,7 +27,7 @@
27 27
     Album, Question, Answer, ComplexSortedPerson, PrePopulatedPostLargeSlug,
28 28
     AdminOrderedField, AdminOrderedModelMethod, AdminOrderedAdminMethod,
29 29
     AdminOrderedCallable, Report, Color2, UnorderedObject, MainPrepopulated,
30  
-    RelatedPrepopulated, UndeletableObject, UserMessenger, Simple)
  30
+    RelatedPrepopulated, UndeletableObject, UserMessenger, Simple, Choice)
31 31
 
32 32
 
33 33
 def callable_year(dt_value):
@@ -615,6 +615,12 @@ def message_extra_tags(self, request, selected):
615 615
         self.message_user(request, "Test tags", extra_tags="extra_tag")
616 616
 
617 617
 
  618
+class ChoiceList(admin.ModelAdmin):
  619
+    list_display = ['choice']
  620
+    readonly_fields = ['choice']
  621
+    fields = ['choice']
  622
+
  623
+
618 624
 site = admin.AdminSite(name="admin")
619 625
 site.register(Article, ArticleAdmin)
620 626
 site.register(CustomArticle, CustomArticleAdmin)
@@ -690,6 +696,7 @@ def message_extra_tags(self, request, selected):
690 696
 site.register(Color2, CustomTemplateFilterColorAdmin)
691 697
 site.register(Simple, AttributeErrorRaisingAdmin)
692 698
 site.register(UserMessenger, MessageTestingAdmin)
  699
+site.register(Choice, ChoiceList)
693 700
 
694 701
 # Register core models we need in our tests
695 702
 from django.contrib.auth.models import User, Group
4  tests/regressiontests/admin_views/models.py
@@ -660,3 +660,7 @@ class Simple(models.Model):
660 660
     """
661 661
     Simple model with nothing on it for use in testing
662 662
     """
  663
+
  664
+class Choice(models.Model):
  665
+    choice = models.CharField(max_length=1, blank=True, null=True,
  666
+        choices=(('y','Yes'), ('n','No'), (None, 'No opinion')))
11  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  
-    Simple, UndeletableObject)
  49
+    Simple, UndeletableObject, Choice)
50 50
 
51 51
 
52 52
 ERROR_MESSAGE = "Please enter the correct username and password \
@@ -3202,6 +3202,15 @@ def test_user_password_change_limited_queryset(self):
3202 3202
         response = self.client.get('/test_admin/admin2/auth/user/%s/password/' % su.pk)
3203 3203
         self.assertEqual(response.status_code, 404)
3204 3204
 
  3205
+    def test_change_form_renders_correct_null_choice_value(self):
  3206
+        """
  3207
+        Regression test for #17911.
  3208
+        """
  3209
+        choice = Choice.objects.create(choice=None)
  3210
+        response = self.client.get('/test_admin/admin/admin_views/choice/%s/' % choice.pk)
  3211
+        self.assertContains(response, '<p>No opinion</p>', html=True)
  3212
+        self.assertNotContains(response, '<p>(None)</p>')
  3213
+
3205 3214
 
3206 3215
 @override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
3207 3216
 class RawIdFieldsTest(TestCase):

0 notes on commit 29d59a8

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