Flask WTForms - option_widget for SelectMultipleField?

Viewed 34

I have a field in my Flask WTForm which uses a SelectMultipleField. I'm using a frontend library which renders the choices fine from the db and adds them to the input field as tags when I select them (like the tags do on this website when creating a question!).

However the data is not being saved, form.field.data and request.form.getlist('field') etc all show None. If I add option_widget=widgets.CheckboxInput to the SelectMultipleField, I can select and save data.

So what I'm wondering is, do I need make a custom field or widget in order for the form to use the selected options as the form data (for example, instead of checking if the field has been checked, it checks if it's in the input field). Going a bit mad reading all the documentation so grateful for a hint in the right direction! Code below:

field = SelectMultipleField(
    "fieldname",
    validators=[Optional()],
    widget=widgets.ListWidget(prefix_label=False),
    # option_widget=CheckboxInput(),
)
1 Answers

please try this way

from flask_appbuilder.fieldwidgets import Select2ManyWidget

"users": QuerySelectMultipleField(
    _("group.user"),
    query_factory=lambda: db.session.query(User),
    widget=Select2ManyWidget(),
    default=[],
),

The result will look like the image below enter image description here

Related