Fixed #17911 -- Ensure that admin readonly fields' display values are shown in change forms when the raw value is None.

This commit is contained in:
Edward Tjörnhammar 2012-11-25 23:13:22 +01:00 committed by Julien Phalip
parent 05dbc07c52
commit 29d59a879e
4 changed files with 23 additions and 5 deletions

View File

@ -186,9 +186,7 @@ class AdminReadonlyField(object):
if getattr(attr, "allow_tags", False): if getattr(attr, "allow_tags", False):
result_repr = mark_safe(result_repr) result_repr = mark_safe(result_repr)
else: else:
if value is None: if isinstance(f.rel, ManyToManyRel) and value is not None:
result_repr = EMPTY_CHANGELIST_VALUE
elif isinstance(f.rel, ManyToManyRel):
result_repr = ", ".join(map(six.text_type, value.all())) result_repr = ", ".join(map(six.text_type, value.all()))
else: else:
result_repr = display_for_field(value, f) result_repr = display_for_field(value, f)

View File

@ -27,7 +27,7 @@ from .models import (Article, Chapter, Account, Media, Child, Parent, Picture,
Album, Question, Answer, ComplexSortedPerson, PrePopulatedPostLargeSlug, Album, Question, Answer, ComplexSortedPerson, PrePopulatedPostLargeSlug,
AdminOrderedField, AdminOrderedModelMethod, AdminOrderedAdminMethod, AdminOrderedField, AdminOrderedModelMethod, AdminOrderedAdminMethod,
AdminOrderedCallable, Report, Color2, UnorderedObject, MainPrepopulated, AdminOrderedCallable, Report, Color2, UnorderedObject, MainPrepopulated,
RelatedPrepopulated, UndeletableObject, UserMessenger, Simple) RelatedPrepopulated, UndeletableObject, UserMessenger, Simple, Choice)
def callable_year(dt_value): def callable_year(dt_value):
@ -615,6 +615,12 @@ class MessageTestingAdmin(admin.ModelAdmin):
self.message_user(request, "Test tags", extra_tags="extra_tag") 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 = admin.AdminSite(name="admin")
site.register(Article, ArticleAdmin) site.register(Article, ArticleAdmin)
site.register(CustomArticle, CustomArticleAdmin) site.register(CustomArticle, CustomArticleAdmin)
@ -690,6 +696,7 @@ site.register(AdminOrderedCallable, AdminOrderedCallableAdmin)
site.register(Color2, CustomTemplateFilterColorAdmin) site.register(Color2, CustomTemplateFilterColorAdmin)
site.register(Simple, AttributeErrorRaisingAdmin) site.register(Simple, AttributeErrorRaisingAdmin)
site.register(UserMessenger, MessageTestingAdmin) site.register(UserMessenger, MessageTestingAdmin)
site.register(Choice, ChoiceList)
# Register core models we need in our tests # Register core models we need in our tests
from django.contrib.auth.models import User, Group from django.contrib.auth.models import User, Group

View File

@ -660,3 +660,7 @@ class Simple(models.Model):
""" """
Simple model with nothing on it for use in testing 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 File

@ -46,7 +46,7 @@ from .models import (Article, BarAccount, CustomArticle, EmptyModel, FooAccount,
OtherStory, ComplexSortedPerson, Parent, Child, AdminOrderedField, OtherStory, ComplexSortedPerson, Parent, Child, AdminOrderedField,
AdminOrderedModelMethod, AdminOrderedAdminMethod, AdminOrderedCallable, AdminOrderedModelMethod, AdminOrderedAdminMethod, AdminOrderedCallable,
Report, MainPrepopulated, RelatedPrepopulated, UnorderedObject, Report, MainPrepopulated, RelatedPrepopulated, UnorderedObject,
Simple, UndeletableObject) Simple, UndeletableObject, Choice)
ERROR_MESSAGE = "Please enter the correct username and password \ ERROR_MESSAGE = "Please enter the correct username and password \
@ -3202,6 +3202,15 @@ class ReadonlyTest(TestCase):
response = self.client.get('/test_admin/admin2/auth/user/%s/password/' % su.pk) response = self.client.get('/test_admin/admin2/auth/user/%s/password/' % su.pk)
self.assertEqual(response.status_code, 404) 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',)) @override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
class RawIdFieldsTest(TestCase): class RawIdFieldsTest(TestCase):