Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[1.0.X] Fixed #9863. A ForeignKey with editable=False to the parent i…

…n an inline no longer raises an exception. Thanks to keithb for the test case and Alex Gaynor for the patch. Backport of r10239 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@10287 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 00087ef006ecbebf34c67d7d15c27a61024ce0da 1 parent 1e0acd5
@jkocherhans jkocherhans authored
View
6 django/forms/models.py
@@ -519,7 +519,11 @@ def add_fields(self, form, index):
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):
"""
View
15 tests/regressiontests/admin_views/models.py
@@ -177,6 +177,20 @@ class PersonaAdmin(admin.ModelAdmin):
BarAccountAdmin
)
+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])
@@ -184,6 +198,7 @@ class PersonaAdmin(admin.ModelAdmin):
admin.site.register(Color)
admin.site.register(Thing, ThingAdmin)
admin.site.register(Persona, PersonaAdmin)
+admin.site.register(Parent, ParentAdmin)
# We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2.
# That way we cover all four cases:
View
17 tests/regressiontests/admin_views/tests.py
@@ -819,3 +819,20 @@ def testInline(self):
self.failUnlessEqual(FooAccount.objects.all()[0].username, "%s-1" % foo_user)
self.failUnlessEqual(BarAccount.objects.all()[0].username, "%s-1" % bar_user)
self.failUnlessEqual(Persona.objects.all()[0].accounts.count(), 2)
+
+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)
Please sign in to comment.
Something went wrong with that request. Please try again.