动态选择WTForms Flask SelectField [英] Dynamic choices 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"
- 如何将我的 groups_list 传递给表单?我试图在FlaskForm模型中实现形成过程,但是看不到current_user对象
- 当我需要将groupID像元组一样传递给SelectField时,是否需要将groupID转换为 string ,然后又转换回 int ?
- 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
- 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().
更正FormModel:
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屋!