动态选择 WTForms Flask SelectField [英] Dynamic choices WTForms Flask SelectField

查看:25
本文介绍了动态选择 WTForms Flask SelectField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 FlaskForms 将 userID 变量传递给 WTForms.首先,我将展示运行良好的代码,然后展示我需要修改的内容(我不知道如何修改的部分).我正在添加与某个组关联的新名称.

I'm trying to pass userID variable to WTForms with FlaskForms. First I'll show code that works fine and then what I need to modify(that the part I don't know how). I'm adding new Name associated with some group.

FlaskForm 模型:

class AddName(FlaskForm):
    name =StringField('Device name', validators=[InputRequired(),Length(min=4, max=30)])
    groupID = SelectField('Payload Type', choices=[(1,"Group1"),(2,"Group2")], validators=[InputRequired])

查看模型:

@app.route('/dashboard/addname', methods=['GET', 'POST'])
def addname():
    form=AddName()
    if form.validate_on_submit():
        name=Name(form.name.data,form.groupID.data)
        db.session.add(name)
        db.session.commit()
        return "New name added"

模板:

              <form method="POST" action="/dashboard/addname">
                  <h2>Add name</h2>
                  {{ form.hidden_tag() }}
                  {{ wtf.form_field(form.name) }}
                  {{ wtf.form_field(form.groupID) }}
                  <button type="submit">Add name</button>
              </form>

我在下拉列表中看到了正确的列表,并且在提交时给了我正确的数字.

I see correct list in dropdown, and on submit gives me correct numbers.

任务:我需要根据current_user.userID传递不同的列表.我正在使用 SQLAlchemy 形成列表,通过从数据库中的表进行查询,所以我的 Flask 视图是:

Task: I need to pass different list based on current_user.userID. I'm forming list using SQLAlchemy, by making query from table from DB, so My Flask view is:

@app.route('/dashboard/addname', methods=['GET', 'POST'])
def addname():
    available_groups=db.session.query(Groups).filter(Groups.userID == currend_user.userID).all()
    #Now forming the list of tuples, so it's ok for SelectField
    groups_list=[(i.groupID, i.groupName) for i in available_groups]
    form=AddName()
    if form.validate_on_submit():
        name=Name(form.name.data,form.groupID.data)
        db.session.add(name)
        db.session.commit()
        return "New name added"

  1. 如何将我的 groups_list 传递给表单?我试图在 FlaskForm 模型中实现形成过程,但它没有看到 current_user 对象
  2. 当我需要像元组一样将 groupID 传递给 SelectField 时,是否需要将 groupID 转换为 string 然后再转换回 int?
  1. How can i pass my groups_list to the form? I tried to implement forming procedure in the FlaskForm model, but it doesn't see current_user object
  2. Do I need to transform groupID to string and then back to int when I need to pass it to the SelectField like tuples?

推荐答案

这里的主要思想是在实例化后将选择列表分配给字段.为此,您需要使用参数 coerce=int.SelectField 的 coerce 关键字 arg 表示我们使用 int() 来强制表单数据.默认强制为 unicode().

The main idea here is to assign the choices list to the field after instantiation. To do so you need to use argument coerce=int. The coerce keyword arg to SelectField says that we use int() to coerce form data. The default coerce is unicode().

正确的表单模型:

class AddName(FlaskForm):
    name =StringField('Device name', validators=[InputRequired(),Length(min=4, max=30)])
    groupID = SelectField('Payload Type', coerce=int, validators=[InputRequired])

正确的看法:

@app.route('/dashboard/addname', methods=['GET', 'POST'])
def addname():
    available_groups=db.session.query(Groups).filter(Groups.userID == currend_user.userID).all()
    #Now forming the list of tuples for SelectField
    groups_list=[(i.groupID, i.groupName) for i in available_groups]
    form=AddName()
    #passing group_list to the form
    form.groupID.choices = groups_list
    if form.validate_on_submit():
        name=Name(form.name.data,form.groupID.data)
        db.session.add(name)
        db.session.commit()
        return "New name added"

这篇关于动态选择 WTForms Flask SelectField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆