From 7a923de42792c5a32c351ae4571d4dd178f08e37 Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Sat, 9 Nov 2024 01:16:16 +0000 Subject: [PATCH] Set sensible defaults for InlinePanel heading and label --- wagtail/admin/panels/inline_panel.py | 5 ++++- wagtail/admin/tests/test_edit_handlers.py | 7 +++++++ 2 files changed, 11 insertions(+), 1 deletion(-) 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"]