Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #35393 -- Added excluded primary keys as hidden fields for the inline admin. #18094

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions django/contrib/admin/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,8 @@ 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 primary key can be editable, but excluded from the inline as well
or self.form._meta.model._meta.pk.name in (self.form._meta.exclude or ())
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
14 changes: 14 additions & 0 deletions tests/admin_inlines/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
Teacher,
Title,
TitleCollection,
UUIDChild,
UUIDParent,
)

site = admin.AdminSite(name="admin")
Expand Down Expand Up @@ -418,6 +420,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 @@ -459,6 +471,8 @@ 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
12 changes: 12 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 @@ -378,3 +379,14 @@ class BothVerboseNameProfile(Profile):
class Meta:
verbose_name = "Model with both - name"
verbose_name_plural = "Model with both - plural name"


class UUIDParent(models.Model):
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
title = models.CharField(max_length=128)


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)
11 changes: 11 additions & 0 deletions tests/admin_inlines/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
SomeChildModel,
SomeParentModel,
Teacher,
UUIDChild,
UUIDParent,
VerboseNamePluralProfile,
VerboseNameProfile,
)
Expand Down Expand Up @@ -113,6 +115,15 @@ 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):
"""Bug #35393."""
uuidparent = UUIDParent.objects.create(title="foo")
UUIDChild.objects.create(title="foo", parent=uuidparent)
response = self.client.get(
reverse("admin:admin_inlines_uuidparent_change", args=(uuidparent.id,))
)
self.assertContains(response, '<input type="hidden" name="uuidchild_set-0-id"')

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