Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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
@edwtjo edwtjo authored jphalip committed
View
4 django/contrib/admin/helpers.py
@@ -186,9 +186,7 @@ def contents(self):
if getattr(attr, "allow_tags", False):
result_repr = mark_safe(result_repr)
else:
- if value is None:
- result_repr = EMPTY_CHANGELIST_VALUE
- elif isinstance(f.rel, ManyToManyRel):
+ if isinstance(f.rel, ManyToManyRel) and value is not None:
result_repr = ", ".join(map(six.text_type, value.all()))
else:
result_repr = display_for_field(value, f)
View
9 tests/regressiontests/admin_views/admin.py
@@ -27,7 +27,7 @@
Album, Question, Answer, ComplexSortedPerson, PrePopulatedPostLargeSlug,
AdminOrderedField, AdminOrderedModelMethod, AdminOrderedAdminMethod,
AdminOrderedCallable, Report, Color2, UnorderedObject, MainPrepopulated,
- RelatedPrepopulated, UndeletableObject, UserMessenger, Simple)
+ RelatedPrepopulated, UndeletableObject, UserMessenger, Simple, Choice)
def callable_year(dt_value):
@@ -615,6 +615,12 @@ def message_extra_tags(self, request, selected):
self.message_user(request, "Test tags", extra_tags="extra_tag")
+class ChoiceList(admin.ModelAdmin):
+ list_display = ['choice']
+ readonly_fields = ['choice']
+ fields = ['choice']
+
+
site = admin.AdminSite(name="admin")
site.register(Article, ArticleAdmin)
site.register(CustomArticle, CustomArticleAdmin)
@@ -690,6 +696,7 @@ def message_extra_tags(self, request, selected):
site.register(Color2, CustomTemplateFilterColorAdmin)
site.register(Simple, AttributeErrorRaisingAdmin)
site.register(UserMessenger, MessageTestingAdmin)
+site.register(Choice, ChoiceList)
# Register core models we need in our tests
from django.contrib.auth.models import User, Group
View
4 tests/regressiontests/admin_views/models.py
@@ -660,3 +660,7 @@ class Simple(models.Model):
"""
Simple model with nothing on it for use in testing
"""
+
+class Choice(models.Model):
+ choice = models.CharField(max_length=1, blank=True, null=True,
+ choices=(('y','Yes'), ('n','No'), (None, 'No opinion')))
View
11 tests/regressiontests/admin_views/tests.py
@@ -46,7 +46,7 @@
OtherStory, ComplexSortedPerson, Parent, Child, AdminOrderedField,
AdminOrderedModelMethod, AdminOrderedAdminMethod, AdminOrderedCallable,
Report, MainPrepopulated, RelatedPrepopulated, UnorderedObject,
- Simple, UndeletableObject)
+ Simple, UndeletableObject, Choice)
ERROR_MESSAGE = "Please enter the correct username and password \
@@ -3202,6 +3202,15 @@ def test_user_password_change_limited_queryset(self):
response = self.client.get('/test_admin/admin2/auth/user/%s/password/' % su.pk)
self.assertEqual(response.status_code, 404)
+ def test_change_form_renders_correct_null_choice_value(self):
+ """
+ Regression test for #17911.
+ """
+ choice = Choice.objects.create(choice=None)
+ response = self.client.get('/test_admin/admin/admin_views/choice/%s/' % choice.pk)
+ self.assertContains(response, '<p>No opinion</p>', html=True)
+ self.assertNotContains(response, '<p>(None)</p>')
+
@override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
class RawIdFieldsTest(TestCase):
Please sign in to comment.
Something went wrong with that request. Please try again.