How to populate WTForms MultiCheckboxField
Categories: Tech | Pubby Cash Received:
It is very tricky to have a MulticheckboxField populated via WTForms with Flask. The field cannot be directly imported. You have to manually make a class using two other imported modules: widgets and SelectMultipleField! See code below:
from flask_wtf import FlaskForm
from wtforms import widgets, SelectMultipleField
class MultiCheckboxField(SelectMultipleField):
widget = widgets.ListWidget(prefix_label=False)
option_widget = widgets.CheckboxInput()
tag = [(0, 'General'), (1, 'Tech'), (2, 'Gaming'), (3, 'YouTube'),
(4, 'Sports'), (5, 'Arts'), (6, 'News'), (7, 'Events'),
(8, 'Education'), (9, 'Market')]
class PostForm(FlaskForm):
tags = MultiCheckboxField('Tags', choices=tag, coerce=int)
Published from: Pennsylvania US
Liked by: Evan Tang, fnfOzvSR