[1.11.x] Fixed #27967 -- Fixed KeyError in admin's inline form with inherited non-editable pk.
Thanks Robin Anupol for the initial report and workaround. Backport of 9dc83c356d363c090f3351c908cad6f823aeb7bf from master
This commit is contained in:
parent
7fb148a638
commit
927d9b51fe
@ -355,7 +355,7 @@ class InlineAdminForm(AdminForm):
|
|||||||
# Also search any parents for an auto field. (The pk info is propagated to child
|
# Also search any parents for an auto field. (The pk info is propagated to child
|
||||||
# models so that does not need to be checked in parents.)
|
# models so that does not need to be checked in parents.)
|
||||||
for parent in self.form._meta.model._meta.get_parent_list():
|
for parent in self.form._meta.model._meta.get_parent_list():
|
||||||
if parent._meta.auto_field:
|
if parent._meta.auto_field or not parent._meta.model._meta.pk.editable:
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@ -26,3 +26,6 @@ Bugfixes
|
|||||||
|
|
||||||
* Fixed model initialization to set the name of class-based model indexes
|
* Fixed model initialization to set the name of class-based model indexes
|
||||||
for models that only inherit ``models.Model`` (:ticket:`28282`).
|
for models that only inherit ``models.Model`` (:ticket:`28282`).
|
||||||
|
|
||||||
|
* Fixed crash in admin's inlines when a model has an inherited non-editable
|
||||||
|
primary key (:ticket:`27967`).
|
||||||
|
@ -5,10 +5,10 @@ from .models import (
|
|||||||
Author, BinaryTree, CapoFamiglia, Chapter, ChildModel1, ChildModel2,
|
Author, BinaryTree, CapoFamiglia, Chapter, ChildModel1, ChildModel2,
|
||||||
Consigliere, EditablePKBook, ExtraTerrestrial, Fashionista, Holder,
|
Consigliere, EditablePKBook, ExtraTerrestrial, Fashionista, Holder,
|
||||||
Holder2, Holder3, Holder4, Inner, Inner2, Inner3, Inner4Stacked,
|
Holder2, Holder3, Holder4, Inner, Inner2, Inner3, Inner4Stacked,
|
||||||
Inner4Tabular, NonAutoPKBook, Novel, ParentModelWithCustomPk, Poll,
|
Inner4Tabular, NonAutoPKBook, NonAutoPKBookChild, Novel,
|
||||||
Profile, ProfileCollection, Question, ReadOnlyInline, ShoppingWeakness,
|
ParentModelWithCustomPk, Poll, Profile, ProfileCollection, Question,
|
||||||
Sighting, SomeChildModel, SomeParentModel, SottoCapo, Title,
|
ReadOnlyInline, ShoppingWeakness, Sighting, SomeChildModel,
|
||||||
TitleCollection,
|
SomeParentModel, SottoCapo, Title, TitleCollection,
|
||||||
)
|
)
|
||||||
|
|
||||||
site = admin.AdminSite(name="admin")
|
site = admin.AdminSite(name="admin")
|
||||||
@ -23,6 +23,11 @@ class NonAutoPKBookTabularInline(admin.TabularInline):
|
|||||||
classes = ('collapse',)
|
classes = ('collapse',)
|
||||||
|
|
||||||
|
|
||||||
|
class NonAutoPKBookChildTabularInline(admin.TabularInline):
|
||||||
|
model = NonAutoPKBookChild
|
||||||
|
classes = ('collapse',)
|
||||||
|
|
||||||
|
|
||||||
class NonAutoPKBookStackedInline(admin.StackedInline):
|
class NonAutoPKBookStackedInline(admin.StackedInline):
|
||||||
model = NonAutoPKBook
|
model = NonAutoPKBook
|
||||||
classes = ('collapse',)
|
classes = ('collapse',)
|
||||||
@ -40,6 +45,7 @@ class AuthorAdmin(admin.ModelAdmin):
|
|||||||
inlines = [
|
inlines = [
|
||||||
BookInline, NonAutoPKBookTabularInline, NonAutoPKBookStackedInline,
|
BookInline, NonAutoPKBookTabularInline, NonAutoPKBookStackedInline,
|
||||||
EditablePKBookTabularInline, EditablePKBookStackedInline,
|
EditablePKBookTabularInline, EditablePKBookStackedInline,
|
||||||
|
NonAutoPKBookChildTabularInline,
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@ -63,6 +63,10 @@ class NonAutoPKBook(models.Model):
|
|||||||
super(NonAutoPKBook, self).save(*args, **kwargs)
|
super(NonAutoPKBook, self).save(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
class NonAutoPKBookChild(NonAutoPKBook):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class EditablePKBook(models.Model):
|
class EditablePKBook(models.Model):
|
||||||
manual_pk = models.IntegerField(primary_key=True)
|
manual_pk = models.IntegerField(primary_key=True)
|
||||||
author = models.ForeignKey(Author, models.CASCADE)
|
author = models.ForeignKey(Author, models.CASCADE)
|
||||||
|
@ -357,6 +357,21 @@ class TestInline(TestDataMixin, TestCase):
|
|||||||
html=True
|
html=True
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_inline_nonauto_noneditable_inherited_pk(self):
|
||||||
|
response = self.client.get(reverse('admin:admin_inlines_author_add'))
|
||||||
|
self.assertContains(
|
||||||
|
response,
|
||||||
|
'<input id="id_nonautopkbookchild_set-0-nonautopkbook_ptr" '
|
||||||
|
'name="nonautopkbookchild_set-0-nonautopkbook_ptr" type="hidden" />',
|
||||||
|
html=True
|
||||||
|
)
|
||||||
|
self.assertContains(
|
||||||
|
response,
|
||||||
|
'<input id="id_nonautopkbookchild_set-2-nonautopkbook_ptr" '
|
||||||
|
'name="nonautopkbookchild_set-2-nonautopkbook_ptr" type="hidden" />',
|
||||||
|
html=True
|
||||||
|
)
|
||||||
|
|
||||||
def test_inline_editable_pk(self):
|
def test_inline_editable_pk(self):
|
||||||
response = self.client.get(reverse('admin:admin_inlines_author_add'))
|
response = self.client.get(reverse('admin:admin_inlines_author_add'))
|
||||||
self.assertContains(
|
self.assertContains(
|
||||||
@ -888,7 +903,7 @@ class SeleniumTests(AdminSeleniumTestCase):
|
|||||||
# One field is in a stacked inline, other in a tabular one.
|
# One field is in a stacked inline, other in a tabular one.
|
||||||
test_fields = ['#id_nonautopkbook_set-0-title', '#id_nonautopkbook_set-2-0-title']
|
test_fields = ['#id_nonautopkbook_set-0-title', '#id_nonautopkbook_set-2-0-title']
|
||||||
show_links = self.selenium.find_elements_by_link_text('SHOW')
|
show_links = self.selenium.find_elements_by_link_text('SHOW')
|
||||||
self.assertEqual(len(show_links), 2)
|
self.assertEqual(len(show_links), 3)
|
||||||
for show_index, field_name in enumerate(test_fields, 0):
|
for show_index, field_name in enumerate(test_fields, 0):
|
||||||
self.wait_until_invisible(field_name)
|
self.wait_until_invisible(field_name)
|
||||||
show_links[show_index].click()
|
show_links[show_index].click()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user