diff --git a/wagtail/admin/panels/inline_panel.py b/wagtail/admin/panels/inline_panel.py index b2e9f3eaea..1469b1e5b0 100644 --- a/wagtail/admin/panels/inline_panel.py +++ b/wagtail/admin/panels/inline_panel.py @@ -3,6 +3,7 @@ import functools from django import forms from django.forms.formsets import DELETION_FIELD_NAME, ORDERING_FIELD_NAME from django.utils.functional import cached_property +from django.utils.text import capfirst from wagtail.admin import compare @@ -26,7 +27,7 @@ class InlinePanel(Panel): super().__init__(*args, **kwargs) self.relation_name = relation_name self.panels = panels - self.heading = heading or label + self.heading = heading or label or capfirst(relation_name.replace("_", " ")) self.label = label self.min_num = min_num self.max_num = max_num @@ -77,6 +78,8 @@ class InlinePanel(Panel): def on_model_bound(self): manager = getattr(self.model, self.relation_name) self.db_field = manager.rel + if not self.label: + self.label = capfirst(self.db_field.related_model._meta.verbose_name) def classes(self): return super().classes() + ["w-panel--nested"] diff --git a/wagtail/admin/tests/test_edit_handlers.py b/wagtail/admin/tests/test_edit_handlers.py index bf1d168193..df261a2bbc 100644 --- a/wagtail/admin/tests/test_edit_handlers.py +++ b/wagtail/admin/tests/test_edit_handlers.py @@ -1523,6 +1523,13 @@ class TestInlinePanel(WagtailTestUtils, TestCase): ), ) + def test_get_heading_and_label_from_field(self): + panel = InlinePanel("social_links").bind_to_model(PersonPage) + # Heading is the plural term, derived from the relation's related_name + self.assertEqual(panel.heading, "Social links") + # Label is the singular term, derived from the related model's verbose_name + self.assertEqual(panel.label, "Social link") + class TestNonOrderableInlinePanel(WagtailTestUtils, TestCase): fixtures = ["test.json"]