diff --git a/django/forms/models.py b/django/forms/models.py index 32a85c5609..70f0631e2c 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -545,7 +545,11 @@ class BaseInlineFormSet(BaseModelFormSet): if self._pk_field == self.fk: form.fields[self._pk_field.name] = InlineForeignKeyField(self.instance, pk_field=True) else: - form.fields[self.fk.name] = InlineForeignKeyField(self.instance, label=form.fields[self.fk.name].label) + # The foreign key field might not be on the form, so we poke at the + # Model field to get the label, since we need that for error messages. + form.fields[self.fk.name] = InlineForeignKeyField(self.instance, + label=getattr(form.fields.get(self.fk.name), 'label', capfirst(self.fk.verbose_name)) + ) def _get_foreign_key(parent_model, model, fk_name=None): """ diff --git a/tests/regressiontests/admin_views/models.py b/tests/regressiontests/admin_views/models.py index 354646344b..8fc3d38690 100644 --- a/tests/regressiontests/admin_views/models.py +++ b/tests/regressiontests/admin_views/models.py @@ -248,6 +248,20 @@ class PodcastAdmin(admin.ModelAdmin): ordering = ('name',) +class Parent(models.Model): + name = models.CharField(max_length=128) + +class Child(models.Model): + parent = models.ForeignKey(Parent, editable=False) + name = models.CharField(max_length=30, blank=True) + +class ChildInline(admin.StackedInline): + model = Child + +class ParentAdmin(admin.ModelAdmin): + model = Parent + inlines = [ChildInline] + admin.site.register(Article, ArticleAdmin) admin.site.register(CustomArticle, CustomArticleAdmin) admin.site.register(Section, inlines=[ArticleInline]) @@ -259,6 +273,7 @@ admin.site.register(Persona, PersonaAdmin) admin.site.register(Subscriber, SubscriberAdmin) admin.site.register(ExternalSubscriber, ExternalSubscriberAdmin) admin.site.register(Podcast, PodcastAdmin) +admin.site.register(Parent, ParentAdmin) # We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2. # That way we cover all four cases: diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py index 2fd4c076b0..bba7f6ac25 100644 --- a/tests/regressiontests/admin_views/tests.py +++ b/tests/regressiontests/admin_views/tests.py @@ -946,3 +946,20 @@ class AdminActionsTest(TestCase): } response = self.client.post('/test_admin/admin/admin_views/externalsubscriber/', action_data) self.failUnlessEqual(response.status_code, 302) + +class TestInlineNotEditable(TestCase): + fixtures = ['admin-views-users.xml'] + + def setUp(self): + result = self.client.login(username='super', password='secret') + self.failUnlessEqual(result, True) + + def tearDown(self): + self.client.logout() + + def test(self): + """ + InlineModelAdmin broken? + """ + response = self.client.get('/test_admin/admin/admin_views/parent/add/') + self.failUnlessEqual(response.status_code, 200)