0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-24 01:57:32 +01:00

Set sensible defaults for InlinePanel heading and label

This commit is contained in:
Matt Westcott 2024-11-09 01:16:16 +00:00 committed by Sage Abdullah
parent 253c401ae2
commit 7a923de427
No known key found for this signature in database
GPG Key ID: EB1A33CC51CC0217
2 changed files with 11 additions and 1 deletions

View File

@ -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"]

View File

@ -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"]