Rendering MultiValueField including help_text and label for each subfield

I am using a JSONField to represent a config that is used in multiple forms. Instead of the default Textarea widget, I want to render multiple fields each with their own label and help_text. I am able to achieve this by implementing a Form just for the config but it seems it should also be possible (and cleaner) to represent the config as a MultiValueField. Unfortunately I cannot for the life of me figure out how to render the label and the help_text when following this approach as that information seems no longer available when the widget is rendered. What am I missing?

class ConfigWidget(forms.MultiWidget):
    template_name = 'config_widget.html'

    def __init__(self, attrs=None):
        widgets = [
            forms.TextInput(attrs=attrs),
            forms.TextInput(attrs=attrs),
        ]
        super().__init__(widgets, attrs)
    
    def decompress(self, value):
        if isinstance(value, dict):
            return [value['foo'], value['bar']]
        return [None, None]


class ConfigField(forms.MultiValueField):
    widget = ConfigWidget
    
    def __init__(self, *args, **kwargs):
        fields = (
            forms.CharField(label='Foo', help_text='Foo help.'),
            forms.CharField(label='Bar', help_text='Bar help.'),
        )
        super().__init__(fields=fields, *args, **kwargs)
    
    def compress(self, data_list):
        if data_list:
            return {
                'foo': data_list[0],
                'bar': data_list[1],
            }
Back to Top