简体   繁体   English

在 flask forms (WTForm) 中的嵌套 FieldList/FormField 中加载数据

[英]Load data in nested FieldList/FormField in flask forms (WTForm)

I have a problem initiating/populating a Form with nested data.我在使用嵌套数据启动/填充表单时遇到问题。

The form is as follow:表格如下:

field_1 --> FieldList(FormField)
    field_1_1 --> StringField
    field_1_2 --> StringField
    field_1_3 --> FieldList(StringField)

I want to instanciate the form using my_form = my_form_class(**data) .我想使用my_form = my_form_class(**data)来实例化表单。

Here is what data looks like:以下是data的样子:

data = {
    "field_1 ": [
        "field_1_1": "abc",
        "field_1_2": "abc",
        "field_1_3": ["abc", "abc", "abc"]
    ]
}

But after after instantiation, my_form.data is empty and my_form.validate() doesn't pass.但是在实例化之后, my_form.data是空的并且my_form.validate()没有通过。

I have other similar forms that are just one level less nested (no FieldList inside a FormField ) and it works fine, could this be the problem?我还有其他类似的FieldList嵌套少一层(在FormField中没有 FieldList )并且工作正常,这可能是问题吗?

This person is having the same problem (no solution). 这个人有同样的问题(没有解决方案)。 From searching around, I see people talking about multiple CSRF tokens?通过搜索,我看到人们在谈论多个 CSRF 令牌? How does this work?这是如何运作的?

Any ideas?有任何想法吗?

The solution was to use:解决方案是使用:

my_form = my_form_class()
my_form.process(data=form_data_json)

instead of:代替:

my_form = my_form_class(**data)

Check this part of the documentation about the .process() method:检查有关.process()方法的文档的这一部分

Since BaseForm does not take its data at instantiation, you must call this to provide form data to the enclosed fields.由于 BaseForm 在实例化时不获取其数据,因此您必须调用它来为封闭的字段提供表单数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM