mirror of
https://github.com/django/django.git
synced 2025-06-01 17:49:12 +00:00
Fixed #35393 -- Added excluded pk as a hidden field to the inline admin.
This commit is contained in:
parent
c7fc9f20b4
commit
2995aeab56
@ -509,6 +509,11 @@ class InlineAdminForm(AdminForm):
|
|||||||
# Auto fields are editable, so check for auto or non-editable pk.
|
# Auto fields are editable, so check for auto or non-editable pk.
|
||||||
self.form._meta.model._meta.auto_field
|
self.form._meta.model._meta.auto_field
|
||||||
or not self.form._meta.model._meta.pk.editable
|
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
|
or
|
||||||
# Also search any parents for an auto field. (The pk info is
|
# Also search any parents for an auto field. (The pk info is
|
||||||
# propagated to child models so that does not need to be checked
|
# propagated to child models so that does not need to be checked
|
||||||
|
@ -57,6 +57,8 @@ from .models import (
|
|||||||
Teacher,
|
Teacher,
|
||||||
Title,
|
Title,
|
||||||
TitleCollection,
|
TitleCollection,
|
||||||
|
UUIDChild,
|
||||||
|
UUIDParent,
|
||||||
)
|
)
|
||||||
|
|
||||||
site = admin.AdminSite(name="admin")
|
site = admin.AdminSite(name="admin")
|
||||||
@ -471,6 +473,16 @@ class ShowInlineChildInline(admin.StackedInline):
|
|||||||
model = ShowInlineChild
|
model = ShowInlineChild
|
||||||
|
|
||||||
|
|
||||||
|
class UUIDChildInline(admin.StackedInline):
|
||||||
|
model = UUIDChild
|
||||||
|
exclude = ("id",)
|
||||||
|
|
||||||
|
|
||||||
|
class UUIDParentModelAdmin(admin.ModelAdmin):
|
||||||
|
model = UUIDParent
|
||||||
|
inlines = [UUIDChildInline]
|
||||||
|
|
||||||
|
|
||||||
class ShowInlineParentAdmin(admin.ModelAdmin):
|
class ShowInlineParentAdmin(admin.ModelAdmin):
|
||||||
def get_inlines(self, request, obj):
|
def get_inlines(self, request, obj):
|
||||||
if obj is not None and obj.show_inlines:
|
if obj is not None and obj.show_inlines:
|
||||||
@ -513,6 +525,7 @@ site.register(CourseProxy, ClassAdminStackedVertical)
|
|||||||
site.register(CourseProxy1, ClassAdminTabularVertical)
|
site.register(CourseProxy1, ClassAdminTabularVertical)
|
||||||
site.register(CourseProxy2, ClassAdminTabularHorizontal)
|
site.register(CourseProxy2, ClassAdminTabularHorizontal)
|
||||||
site.register(ShowInlineParent, ShowInlineParentAdmin)
|
site.register(ShowInlineParent, ShowInlineParentAdmin)
|
||||||
|
site.register(UUIDParent, UUIDParentModelAdmin)
|
||||||
# Used to test hidden fields in tabular and stacked inlines.
|
# Used to test hidden fields in tabular and stacked inlines.
|
||||||
site2 = admin.AdminSite(name="tabular_inline_hidden_field_admin")
|
site2 = admin.AdminSite(name="tabular_inline_hidden_field_admin")
|
||||||
site2.register(SomeParentModel, inlines=[ChildHiddenFieldTabularInline])
|
site2.register(SomeParentModel, inlines=[ChildHiddenFieldTabularInline])
|
||||||
|
@ -3,6 +3,7 @@ Testing of admin inline formsets.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import random
|
import random
|
||||||
|
import uuid
|
||||||
|
|
||||||
from django.contrib.contenttypes.fields import GenericForeignKey
|
from django.contrib.contenttypes.fields import GenericForeignKey
|
||||||
from django.contrib.contenttypes.models import ContentType
|
from django.contrib.contenttypes.models import ContentType
|
||||||
@ -399,3 +400,13 @@ class BothVerboseNameProfile(Profile):
|
|||||||
class Meta:
|
class Meta:
|
||||||
verbose_name = "Model with both - name"
|
verbose_name = "Model with both - name"
|
||||||
verbose_name_plural = "Model with both - plural 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)
|
||||||
|
@ -44,6 +44,8 @@ from .models import (
|
|||||||
SomeChildModel,
|
SomeChildModel,
|
||||||
SomeParentModel,
|
SomeParentModel,
|
||||||
Teacher,
|
Teacher,
|
||||||
|
UUIDChild,
|
||||||
|
UUIDParent,
|
||||||
VerboseNamePluralProfile,
|
VerboseNamePluralProfile,
|
||||||
VerboseNameProfile,
|
VerboseNameProfile,
|
||||||
)
|
)
|
||||||
@ -115,6 +117,19 @@ class TestInline(TestDataMixin, TestCase):
|
|||||||
)
|
)
|
||||||
self.assertContains(response, "<label>Inner readonly label:</label>")
|
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):
|
def test_many_to_many_inlines(self):
|
||||||
"Autogenerated many-to-many inlines are displayed correctly (#13407)"
|
"Autogenerated many-to-many inlines are displayed correctly (#13407)"
|
||||||
response = self.client.get(reverse("admin:admin_inlines_author_add"))
|
response = self.client.get(reverse("admin:admin_inlines_author_add"))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user