Code

Ticket #9863: 9683.2.diff

File 9683.2.diff, 3.1 KB (added by Alex, 5 years ago)
Line 
1diff --git a/django/forms/models.py b/django/forms/models.py
2index 32a85c5..cc8bea5 100644
3--- a/django/forms/models.py
4+++ b/django/forms/models.py
5@@ -545,7 +545,11 @@ class BaseInlineFormSet(BaseModelFormSet):
6         if self._pk_field == self.fk:
7             form.fields[self._pk_field.name] = InlineForeignKeyField(self.instance, pk_field=True)
8         else:
9-            form.fields[self.fk.name] = InlineForeignKeyField(self.instance, label=form.fields[self.fk.name].label)
10+            # the foreign key field might not be on the form, so we poke at the
11+            # Model Field to get the label, since we need that for error messages
12+            form.fields[self.fk.name] = InlineForeignKeyField(self.instance,
13+                label=getattr(form.fields.get(self.fk.name), 'label', capfirst(self.fk.verbose_name))
14+            )
15 
16 def _get_foreign_key(parent_model, model, fk_name=None):
17     """
18diff --git a/tests/regressiontests/admin_views/models.py b/tests/regressiontests/admin_views/models.py
19index 3546463..b4e64cf 100644
20--- a/tests/regressiontests/admin_views/models.py
21+++ b/tests/regressiontests/admin_views/models.py
22@@ -248,6 +248,23 @@ class PodcastAdmin(admin.ModelAdmin):
23 
24     ordering = ('name',)
25 
26+class Parent(models.Model):
27+    name = models.CharField(max_length = 128, db_index = True)
28+
29+class Child(models.Model):
30+    parent = models.ForeignKey(Parent, related_name = 'child_set', editable = False)
31+    name = models.CharField(max_length = 30, blank = True)
32+
33+class ChildInline(admin.StackedInline):
34+    model = Child
35+    extra = 10
36+
37+class ParentAdmin(admin.ModelAdmin):
38+    model = Parent
39+    inlines = [
40+       ChildInline,
41+    ]
42+
43 admin.site.register(Article, ArticleAdmin)
44 admin.site.register(CustomArticle, CustomArticleAdmin)
45 admin.site.register(Section, inlines=[ArticleInline])
46@@ -259,6 +276,7 @@ admin.site.register(Persona, PersonaAdmin)
47 admin.site.register(Subscriber, SubscriberAdmin)
48 admin.site.register(ExternalSubscriber, ExternalSubscriberAdmin)
49 admin.site.register(Podcast, PodcastAdmin)
50+admin.site.register(Parent, ParentAdmin)
51 
52 # We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2.
53 # That way we cover all four cases:
54diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py
55index 2fd4c07..d42be66 100644
56--- a/tests/regressiontests/admin_views/tests.py
57+++ b/tests/regressiontests/admin_views/tests.py
58@@ -946,3 +946,20 @@ class AdminActionsTest(TestCase):
59         }
60         response = self.client.post('/test_admin/admin/admin_views/externalsubscriber/', action_data)
61         self.failUnlessEqual(response.status_code, 302)
62+
63+class TestInlineNotEditable(TestCase):
64+    fixtures = ['admin-views-users.xml', ]
65+
66+    def setUp(self):
67+        result = self.client.login(username='super', password='secret')
68+        self.failUnlessEqual(result, True)
69+
70+    def tearDown(self):
71+        self.client.logout()
72+
73+    def test(self):
74+        """
75+        InlineModelAdmin broken?
76+        """
77+        response = self.client.get('/test_admin/admin/admin_views/parent/add/')
78+        self.failUnlessEqual(response.status_code, 200)