Wagtail: How to set InlinePanel Child Label based on model fields?

How can I set the label on the InlinePanel child ("Field 1") to a value based on fields of a model being added? InlinePanel Form fields - Field N

My models:

class FormField(AbstractFormField):
    label = models.CharField(
        blank=True,
        verbose_name=_("label"),
        max_length=255,
        help_text=_("The label of the form field"),
    )
    field_type = models.CharField(
        verbose_name="field type",
        max_length=16,
        choices=(("section", "Section"),) + FORM_FIELD_CHOICES,
    )
    page = ParentalKey(
        'ContactPageBase',
        on_delete=models.CASCADE,
        related_name='form_fields',
    )

And contact form page:

class ContactPageBase(WagtailCaptchaEmailForm, BasePage):
    ...
    content_panels = AbstractEmailForm.content_panels + [
        FieldPanel('contact_intro'),
        InlinePanel('form_fields', heading=_('Form fields'), label=_("Field")),
        FieldPanel('thank_you_text')
    ]

I understand the label can be edited like above but that is for the whole set of children in the InlinePanel but I would like to show something like "{FormField.field_type} - {FormField.label}" in the label so it's clear in the overview on the right. How can this be achieved?

To set the label on the InlinePanel child dynamically based on the fields of the model being added, you can override the str method in your FormField model.

class FormField(AbstractFormField):
    ...

    def __str__(self):
        return f"{self.get_field_type_display()} - {self.label}"

class ContactPageBase(Page):
    ...

    content_panels = [
        FieldPanel('contact_intro'),
        InlinePanel('form_fields', label=_("Field")),
        FieldPanel('thank_you_text')
    ]
Back to Top