In a wagtail settings model I have a CharField
-based choice field and want that to behave as a multi select in Wagtails FieldPanel
. The choices are rendered correctly as multiple checkboxes, but on submit the form validation will raise an error:
Select a valid choice. ['OPT1'] is not one of the available choices.
So how do I use text-based choices as a multi select in Wagtails FieldPanel
?
Do I have to override the form field type to a forms.fields.MultipleChoiceField
somehow?
Setting the widget
attribute to Select
- instead of CheckboxSelectMultiple
- the correctly rendered and populated select dropdown widget works as expected.
My current implementation looks (roughly) like this:
# models.py
@register_setting(icon='cog')
class MySettings(BaseGenericSetting):
class MyChoices(models.TextChoices):
OPT_1 = "OPT1"
OPT_2 = "OPT2"
choicefield = models.CharField(
max_length=4,
blank=True,
choices=MyChoices.choices,
)
panels = [
FieldPanel(
'choicefield',
widget=forms.CheckboxSelectMultiple,
),
]
I did not find any hints in the Customising generated forms docs.