Flask / WTForms:动态RadioField的正确方法

问题描述 投票:0回答:1

我有使用WTForms创建动态RadioField的问题...

当我尝试基本的例子时:

targeting_type = RadioField('Label', choices=[('value', 'description'),
                                              ('value_two', 'whatever')])

一切正常。

当我尝试使用这个例子:Flask-SQLAlchemy wtform based on db

in form是来自DB的值。但是,当我点击提交按钮时,页面被“重新加载”但可能“没有数据”。

我的views.py示例:

form = TargetingTypeForm()
form.targeting_type.choices = [
    (targeting_type.id, targeting_type.name)
    for targeting_type in SettingsTargetingType.query.all()]

if form.validate_on_submit():
    print('test', form.targeting_type.data)

提交此表后,测试数据不打印:/

请问使用WTForms + SQLAlchemy查询创建RadioField的正确方法是什么?

谢谢你的回答。

python-3.x flask sqlalchemy flask-wtforms
1个回答
1
投票

由于你使用ID作为值,我猜它是一个整数,你必须在你的coerse上使用RadioField属性!

试试这个:

form = TargetingTypeForm()
form.targeting_type.choices = [
    (targeting_type.id, targeting_type.name)
    for targeting_type in SettingsTargetingType.query.all()]
form.targeting_type.coerse = int
if form.validate_on_submit():
    print('test', form.targeting_type.data)

或者将qazxsw poi添加到qazxsw poi类中的qazxsw poi定义中

© www.soinside.com 2019 - 2024. All rights reserved.