简体   繁体   English

使用WTForms(或FormEncode)嵌套对象?

[英]Nested objects using WTForms (or FormEncode)?

What's the best practice for creating a form with nested objects/fields? 创建带有嵌套对象/字段的表单的最佳实践是什么?

I have a Team object, with a field "name", and a team can consist of 1 to many (lets say 12) members, each of whom has a name, an e-mail, age, t-shirt size etc. I'd like to validate all of them in one go. 我有一个“团队”对象,其字段为“名称”,一个团队可以由1到许多(比如说12)成员组成,每个成员都有一个名称,电子邮件,年龄,T恤衫尺寸等。想一口气验证所有这些。 So that I would post all the team members as an array.. the field names would be something like members[0].name members[1].name or whatever the form validator would be able to parse. 这样我就将所有团队成员发布为一个数组。字段名称将类似于member [0] .name Members [1] .name或任何形式验证器可以解析的名称。

Create one Form for the Team and one Form for a Member. 为团队创建一个表单,为成员创建一个表单。 In the Team-form, create a FieldList of FormFields: 在团队表单中,创建FormFields的FieldList:

class Member(Form):
    name = StringField("Name")
    ...

class Team(Form):
    ...
    members = FieldList(FormField(Member))

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

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