July 2019
Beginner to intermediate
302 pages
9h 38m
English
In our previous recipe, we created CategoryField. This field used the Select widget, which was provided by the Select superclass. Let's replace the Select widget with a radio input in models.py:
from wtforms.widgets import html_params, Select, HTMLString
class CustomCategoryInput(Select):
def __call__(self, field, **kwargs):
kwargs.setdefault('id', field.id)
html = []
for val, label, selected in field.iter_choices():
html.append(
'<input type="radio" %s> %s' % (
html_params(
name=field.name, value=val, checked=selected, **kwargs
), label
)
)
return HTMLString(' '.join(html))
class CategoryField(SelectField):
widget = CustomCategoryInput()
# Rest of the code remains same as in last recipe Creating custom field and validation ...