在 wtforms 如何以真实形式使用文档中的示例?

In wtforms How to use the example from the docs in a real form?

wtform docuentation 中,以下函数被描述为可以将 SelectMultipleField 呈现为复选框集合的小部件:

def select_multi_checkbox(field, ul_class='', **kwargs):
    kwargs.setdefault('type', 'checkbox')
    field_id = kwargs.pop('id', field.id)
    html = [u'<ul %s>' % html_params(id=field_id, class_=ul_class)]
    for value, label, checked in field.iter_choices():
        choice_id = u'%s-%s' % (field_id, value)
        options = dict(kwargs, name=field.name, value=value, id=choice_id)
        if checked:
            options['checked'] = 'checked'
        html.append(u'<li><input %s /> ' % html_params(**options))
        html.append(u'<label for="%s">%s</label></li>' % (field_id, label))
    html.append(u'</ul>')
    return u''.join(html)

我正在尝试实际使用它作为示例,以查看它在我的一个表单中的样子。但是我遇到了一些麻烦,我不确定如何调用它,因为我只习惯使用默认字段。这是我试过的:

class myForm(FlaskForm):
    my_choices = [('1', 'Choice1'), ('2', 'Choice2'), ('3', 'Choice3')]
    my_multi_select = SelectMultipleField(choices = my_choices,id='Test')
    my_checkbox_multi_select = select_multi_checkbox(my_multi_select)

这给了我以下错误:

line 52, in select_multi_checkbox
field_id = kwargs.pop('id', field.id)
AttributeError: 'UnboundField' object has no attribute 'id'

我的下一次迭代是这样的:

my_checkbox_multi_select = select_multi_checkbox(SelectMultipleField,ul_class='',choices=my_choices,id='test')

这也给了我 id 属性错误,但该字段不再是 Unbounded。

line 52, in select_multi_checkbox
field_id = kwargs.pop('id', field.id)
AttributeError: type object 'SelectMultipleField' has no attribute 'id'

我想知道实现它的正确方法是什么。我看到它是一个函数,所以我认为它需要在一个字段上调用,但我不确定我做错了什么。

所以我想通了。我猜它在文档中,但我有一段时间不清楚。这是我渲染这个字段所做的。我想这应该是显而易见的,但如果其他人正在这里寻找它,那就是:

tester = SelectMultipleField(choices=my_choices, widget=select_multi_checkbox)

只需将其称为小部件即可。很简单。我希望我早点把它放在一起。