From 84de614529f2035057d2d518b7ad94febebcc807 Mon Sep 17 00:00:00 2001 From: Gary Wilson Jr Date: Fri, 26 Mar 2010 23:38:05 +0000 Subject: [PATCH] Fixed #13174 -- Fixed missing field label for `readonly_fields` when used in `StackedInline`, thanks to benc for the report and ptone for the investigation and initial patch. * Corrected `InlineAdminForm.__init__` to pass along `model_admin` parameter in `super` call. * Lookup the field label in the form's model, not the `model_admin` model. git-svn-id: http://code.djangoproject.com/svn/django/trunk@12857 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/admin/helpers.py | 4 ++-- tests/regressiontests/admin_inlines/models.py | 2 ++ tests/regressiontests/admin_inlines/tests.py | 12 +++++++++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/django/contrib/admin/helpers.py b/django/contrib/admin/helpers.py index b969653317..33050c3cd4 100644 --- a/django/contrib/admin/helpers.py +++ b/django/contrib/admin/helpers.py @@ -140,7 +140,7 @@ class AdminReadonlyField(object): if not self.is_first: attrs["class"] = "inline" name = forms.forms.pretty_name( - label_for_field(self.field, self.model_admin.model, self.model_admin) + label_for_field(self.field, self.form._meta.model, self.model_admin) ) contents = force_unicode(escape(name)) + u":" return mark_safe('%(contents)s' % { @@ -232,7 +232,7 @@ class InlineAdminForm(AdminForm): self.original_content_type_id = ContentType.objects.get_for_model(original).pk self.show_url = original and hasattr(original, 'get_absolute_url') super(InlineAdminForm, self).__init__(form, fieldsets, prepopulated_fields, - readonly_fields) + readonly_fields, model_admin) def __iter__(self): for name, options in self.fieldsets: diff --git a/tests/regressiontests/admin_inlines/models.py b/tests/regressiontests/admin_inlines/models.py index 9b491eb939..2c6a64dc7a 100644 --- a/tests/regressiontests/admin_inlines/models.py +++ b/tests/regressiontests/admin_inlines/models.py @@ -38,11 +38,13 @@ class Holder(models.Model): class Inner(models.Model): dummy = models.IntegerField() holder = models.ForeignKey(Holder) + readonly = models.CharField("Inner readonly label", max_length=1) class InnerInline(admin.StackedInline): model = Inner can_delete = False + readonly_fields = ('readonly',) # For bug #13174 tests. class Holder2(models.Model): diff --git a/tests/regressiontests/admin_inlines/tests.py b/tests/regressiontests/admin_inlines/tests.py index 9207c5521c..1ac0100b4a 100644 --- a/tests/regressiontests/admin_inlines/tests.py +++ b/tests/regressiontests/admin_inlines/tests.py @@ -4,6 +4,7 @@ from django.test import TestCase from models import Holder, Inner, InnerInline from models import Holder2, Inner2, Holder3, Inner3 + class TestInline(TestCase): fixtures = ['admin-views-users.xml'] @@ -29,6 +30,15 @@ class TestInline(TestCase): actual = inner_formset.can_delete self.assertEqual(expected, actual, 'can_delete must be equal') + def test_readonly_stacked_inline_label(self): + """Bug #13174.""" + holder = Holder.objects.create(dummy=42) + inner = Inner.objects.create(holder=holder, dummy=42, readonly='') + response = self.client.get('/test_admin/admin/admin_inlines/holder/%i/' + % holder.id) + self.assertContains(response, '') + + class TestInlineMedia(TestCase): fixtures = ['admin-views-users.xml'] @@ -63,4 +73,4 @@ class TestInlineMedia(TestCase): change_url = '/test_admin/admin/admin_inlines/holder2/%i/' % holder.id response = self.client.get(change_url) self.assertContains(response, 'my_awesome_admin_scripts.js') - self.assertContains(response, 'my_awesome_inline_scripts.js') \ No newline at end of file + self.assertContains(response, 'my_awesome_inline_scripts.js')