[英]Flask WTForms SelectField Create choices from api data
I am pulling information from an api that is delivered like this:我正在从这样传递的 api 中提取信息:
{
"Location": {
"Units": [
{
"id": 1,
"Item": [
{
"Description": "some string value",
"ID": "some string value"
},
{
"Description": "some string value",
"ID": "some string value"
},
{
"Description": "some string value",
"ID": "some string value"
}
]
},
{
"id": 2,
"Item": [
{
"Description": "some string value",
"ID": "some string value"
},
{
"Description": "some string value",
"ID": "some string value"
},
{
"Description": "some string value",
"ID": "some string value"
}
]
}
]
}
}
I am providing a wtform with a select field:我正在提供一个带有 select 字段的 wtform:
forms.py forms.py
class ReserveForm(Form):
item = SelectField(
'Item',
choices=[],
coerce=int
)
I am passing the id of the "Unit.id" to my route as a url variable:我将“Unit.id”的 ID 作为 url 变量传递给我的路线:
app.py应用程序.py
@app.route('/reserve/<id>')
def reserve(id):
data = *my api data in json format*
form = ReserveForm()
return render_template('reserve.html', data=data, form=form)
and in my view I am printing the form:在我看来,我正在打印表格:
<form action="" method="POST">
{{ form.hidden_tag() }}
{{ form.item.label() }}
{{ form.item() }}
</form>
What I need to do is use the 'Item's, from the 'Unit' with the ID that matches the id from the url variable, as the choices for the select field.我需要做的是使用 ID 与 url 变量中的 ID 相匹配的“Unit”中的“Item”作为 select 字段的选项。
I cannot seam to find a way to add choices to the field at the template level, which would be my preferred method.我无法找到一种在模板级别向字段添加选项的方法,而这将是我的首选方法。 Something like:
就像是:
<form action="" method="POST">
{{ form.hidden_tag() }}
{{ form.item.label() }}
{{ form.item(choices=[data.Location.Unit[specific-ID].item]) }}
</form>
I was banking on finding a solution like this, because I am most familiar with Jinja2.我寄希望于找到这样的解决方案,因为我最熟悉 Jinja2。
Since I cannot find a way to make that work, I have been trying to figure out how to create the selectfield choices in the route.由于我找不到实现该功能的方法,因此我一直在尝试弄清楚如何在路线中创建选择字段选项。 This posts was helpful: Dynamic choices WTForms Flask SelectField
这篇文章很有帮助: 动态选择 WTForms Flask SelectField
however my data is structured differently because there is an extra nested layer.但是我的数据结构不同,因为有一个额外的嵌套层。
I think I am getting it right, by looping through the 'Units' to find the one with an id matching my variable, and assigning that 'Unit' to a variable.我想我做对了,通过循环遍历“单位”以找到 ID 与我的变量匹配的那个,并将该“单位”分配给一个变量。 Then I can do like on the post linked above, using a key value for loop (I do not know what this is called or how it works so I am not able to search for how it works):
然后我可以像上面链接的帖子一样,使用循环的键值(我不知道这叫什么或它是如何工作的,所以我无法搜索它是如何工作的):
@app.route('/reserve/<id>')
def reserve(id):
data = *my api data in json format*
for item in data['Location']['Unit']:
if item['ID'] == id:
unit = item
items = [(item['ID'], item['Description']) for item in unit['Item']]
form = ReserveForm()
form.item.choices = items
return render_template('reserve.html', data=data, form=form)
I am pretty sure this is going to work, but I am receiving an error 'alueError: invalid literal for int() with base 10:'我很确定这会起作用,但我收到错误消息“alueError:以 10 为基数的 int() 的无效文字:”
obviously the key needs to be an integer, not a string.显然密钥需要是 integer,而不是字符串。 The problem is that both the description and ID from the api data are strings, and the id is a letter/number combo that can't be converted to an integer.
问题在于 api 数据中的描述和 ID 都是字符串,而 ID 是字母/数字组合,无法转换为 integer。
I need the description to be the display text for the select field option, and I need the value to be the ID string.我需要描述作为 select 字段选项的显示文本,我需要值作为 ID 字符串。
what can I use as the key if I don't have an integer ID?如果我没有 integer ID,我可以使用什么作为密钥? how do the name and value apply to the select field options if I can't use the ID string as the key?
如果我不能使用 ID 字符串作为键,名称和值如何应用于 select 字段选项? I am assuming the key will be the select field value.
我假设键是 select 字段值。
**UPDATE: I replaced item['ID'] with the number '1': **更新:我用数字“1”替换了项目['ID']:
items = [(1, item['Description']) for item in unit['Item']]
and now the page is rendering, and the select field is working, but all of the select field values are set to '1':现在页面正在呈现,select 字段正在工作,但所有 select 字段值都设置为“1”:
<option value="1">item name from api data</option>
The value needs to be the ID of the item form the api data, and that looks like:该值需要是 api 数据中的项目 ID,如下所示:
'ID': 'babb87d5-323f-4d56-9ed0-e2b643a78936'
but it doesn't seam to be possible to use a string as the value.但不可能使用字符串作为值。 any advice would be appreciated.
任何意见,将不胜感激。
I was forcing an int value by setting coerce to int in my field settings.我通过在我的字段设置中将 coerce 设置为 int 来强制使用 int 值。 The default for coerce is unicode. Once I changed that the field populated as it is supposed.
coerce 的默认值是 unicode。一旦我更改了该字段,就会按预期填充。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.