From 16afffffe87bf8b7de6403329937433ac5096697 Mon Sep 17 00:00:00 2001 From: Ben Davis Date: Tue, 18 Feb 2014 16:44:33 -0600 Subject: [PATCH] Fixed #22087 -- Made AdminReadonlyField respect ModelForm Meta overrides. --- django/contrib/admin/helpers.py | 14 ++++++++++++-- tests/admin_views/admin.py | 19 ++++++++++++++++++- tests/admin_views/models.py | 7 +++++++ tests/admin_views/tests.py | 14 +++++++++++++- 4 files changed, 50 insertions(+), 4 deletions(-) diff --git a/django/contrib/admin/helpers.py b/django/contrib/admin/helpers.py index bf80ac5d48e..4f6bbe51e4e 100644 --- a/django/contrib/admin/helpers.py +++ b/django/contrib/admin/helpers.py @@ -136,7 +136,6 @@ class AdminField(object): class AdminReadonlyField(object): def __init__(self, form, field, is_first, model_admin=None): - label = label_for_field(field, form._meta.model, model_admin) # Make self.field look a little bit like a field. This means that # {{ field.name }} must be a useful class name to identify the field. # For convenience, store other field-related data here too. @@ -144,11 +143,22 @@ class AdminReadonlyField(object): class_name = field.__name__ if field.__name__ != '' else '' else: class_name = field + + if form._meta.labels and class_name in form._meta.labels: + label = form._meta.labels[class_name] + else: + label = label_for_field(field, form._meta.model, model_admin) + + if form._meta.help_texts and class_name in form._meta.help_texts: + help_text = form._meta.help_texts[class_name] + else: + help_text = help_text_for_field(class_name, form._meta.model) + self.field = { 'name': class_name, 'label': label, + 'help_text': help_text, 'field': field, - 'help_text': help_text_for_field(class_name, form._meta.model) } self.form = form self.model_admin = model_admin diff --git a/tests/admin_views/admin.py b/tests/admin_views/admin.py index a392daf2938..cd9ce2bf17b 100644 --- a/tests/admin_views/admin.py +++ b/tests/admin_views/admin.py @@ -35,7 +35,7 @@ from .models import (Article, Chapter, Child, Parent, Picture, Widget, UnchangeableObject, UserMessenger, Simple, Choice, ShortMessage, Telegram, FilteredManager, EmptyModelHidden, EmptyModelVisible, EmptyModelMixin, State, City, Restaurant, Worker, ParentWithDependentChildren, - DependentChild, StumpJoke) + DependentChild, StumpJoke, FieldOverridePost) def callable_year(dt_value): @@ -435,6 +435,22 @@ class PostAdmin(admin.ModelAdmin): value.short_description = 'Value in $US' +class FieldOverridePostForm(forms.ModelForm): + model = FieldOverridePost + + class Meta: + help_texts = { + 'posted': 'Overridden help text for the date', + } + labels = { + 'public': 'Overridden public label', + } + + +class FieldOverridePostAdmin(PostAdmin): + form = FieldOverridePostForm + + class CustomChangeList(ChangeList): def get_queryset(self, request): return self.root_queryset.filter(pk=9999) # Does not exist @@ -833,6 +849,7 @@ site.register(Recommender) site.register(Collector, CollectorAdmin) site.register(Category, CategoryAdmin) site.register(Post, PostAdmin) +site.register(FieldOverridePost, FieldOverridePostAdmin) site.register(Gadget, GadgetAdmin) site.register(Villain) site.register(SuperVillain) diff --git a/tests/admin_views/models.py b/tests/admin_views/models.py index c729f6b90a9..fb72b12717b 100644 --- a/tests/admin_views/models.py +++ b/tests/admin_views/models.py @@ -438,6 +438,13 @@ class Post(models.Model): return "Very awesome." +# Proxy model to test overridden fields attrs on Post model so as not to +# interfere with other tests. +class FieldOverridePost(Post): + class Meta: + proxy = True + + @python_2_unicode_compatible class Gadget(models.Model): name = models.CharField(max_length=100) diff --git a/tests/admin_views/tests.py b/tests/admin_views/tests.py index a88117fa6f8..39a79c06140 100644 --- a/tests/admin_views/tests.py +++ b/tests/admin_views/tests.py @@ -52,7 +52,7 @@ from .models import (Article, BarAccount, CustomArticle, EmptyModel, FooAccount, Report, MainPrepopulated, RelatedPrepopulated, UnorderedObject, Simple, UndeletableObject, UnchangeableObject, Choice, ShortMessage, Telegram, Pizza, Topping, FilteredManager, City, Restaurant, Worker, - ParentWithDependentChildren, Character) + ParentWithDependentChildren, Character, FieldOverridePost) from .admin import site, site2, CityAdmin @@ -3693,6 +3693,18 @@ class ReadonlyTest(TestCase): response = self.client.get('/test_admin/admin/admin_views/topping/add/') self.assertEqual(response.status_code, 200) + def test_readonly_field_overrides(self): + """ + Regression test for #22087 - ModelForm Meta overrides are ignored by + AdminReadonlyField + """ + p = FieldOverridePost.objects.create(title="Test Post", content="Test Content") + response = self.client.get('/test_admin/admin/admin_views/fieldoverridepost/%d/' % p.pk) + self.assertEqual(response.status_code, 200) + self.assertContains(response, '

Overridden help text for the date

') + self.assertContains(response, '', html=True) + self.assertNotContains(response, "Some help text for the date (with unicode ŠĐĆŽćžšđ)") + @override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',)) class LimitChoicesToInAdminTest(TestCase):