Skip to content

Commit

Permalink
Fixed #35393 -- Added excluded pk as a hidden field to the inline admin.
Browse files Browse the repository at this point in the history
  • Loading branch information
KommuSoft authored and sarahboyce committed May 22, 2024
1 parent c7fc9f2 commit 2995aea
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
5 changes: 5 additions & 0 deletions django/contrib/admin/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,11 @@ def needs_explicit_pk_field(self):
# Auto fields are editable, so check for auto or non-editable pk.
self.form._meta.model._meta.auto_field
or not self.form._meta.model._meta.pk.editable
# The pk can be editable, but excluded from the inline.
or (
self.form._meta.exclude
and self.form._meta.model._meta.pk.name in self.form._meta.exclude
)
or
# Also search any parents for an auto field. (The pk info is
# propagated to child models so that does not need to be checked
Expand Down
13 changes: 13 additions & 0 deletions tests/admin_inlines/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
Teacher,
Title,
TitleCollection,
UUIDChild,
UUIDParent,
)

site = admin.AdminSite(name="admin")
Expand Down Expand Up @@ -471,6 +473,16 @@ class ShowInlineChildInline(admin.StackedInline):
model = ShowInlineChild


class UUIDChildInline(admin.StackedInline):
model = UUIDChild
exclude = ("id",)


class UUIDParentModelAdmin(admin.ModelAdmin):
model = UUIDParent
inlines = [UUIDChildInline]


class ShowInlineParentAdmin(admin.ModelAdmin):
def get_inlines(self, request, obj):
if obj is not None and obj.show_inlines:
Expand Down Expand Up @@ -513,6 +525,7 @@ def get_inlines(self, request, obj):
site.register(CourseProxy1, ClassAdminTabularVertical)
site.register(CourseProxy2, ClassAdminTabularHorizontal)
site.register(ShowInlineParent, ShowInlineParentAdmin)
site.register(UUIDParent, UUIDParentModelAdmin)
# Used to test hidden fields in tabular and stacked inlines.
site2 = admin.AdminSite(name="tabular_inline_hidden_field_admin")
site2.register(SomeParentModel, inlines=[ChildHiddenFieldTabularInline])
Expand Down
11 changes: 11 additions & 0 deletions tests/admin_inlines/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import random
import uuid

from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
Expand Down Expand Up @@ -399,3 +400,13 @@ class BothVerboseNameProfile(Profile):
class Meta:
verbose_name = "Model with both - name"
verbose_name_plural = "Model with both - plural name"


class UUIDParent(models.Model):
pass


class UUIDChild(models.Model):
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
title = models.CharField(max_length=128)
parent = models.ForeignKey(UUIDParent, on_delete=models.CASCADE)
15 changes: 15 additions & 0 deletions tests/admin_inlines/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
SomeChildModel,
SomeParentModel,
Teacher,
UUIDChild,
UUIDParent,
VerboseNamePluralProfile,
VerboseNameProfile,
)
Expand Down Expand Up @@ -115,6 +117,19 @@ def test_readonly_stacked_inline_label(self):
)
self.assertContains(response, "<label>Inner readonly label:</label>")

def test_excluded_id_for_inlines_uses_hidden_field(self):
parent = UUIDParent.objects.create()
child = UUIDChild.objects.create(title="foo", parent=parent)
response = self.client.get(
reverse("admin:admin_inlines_uuidparent_change", args=(parent.id,))
)
self.assertContains(
response,
f'<input type="hidden" name="uuidchild_set-0-id" value="{child.id}" '
'id="id_uuidchild_set-0-id">',
html=True,
)

def test_many_to_many_inlines(self):
"Autogenerated many-to-many inlines are displayed correctly (#13407)"
response = self.client.get(reverse("admin:admin_inlines_author_add"))
Expand Down

0 comments on commit 2995aea

Please sign in to comment.