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:
parent
253c401ae2
commit
7a923de427
@ -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"]
|
||||
|
@ -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"]
|
||||
|
Loading…
Reference in New Issue
Block a user