WTForms - SelectMultipleField 的多值字符串
我有一个名为 TestForm
的 WTForm,具有以下属性:
areas = SelectMultipleField(u'Test Areas', choices=TestArea.names())
当我创建 TestForm
的新实例并传入具有 areas
属性的对象时,该对象没有 areas
的值列表,而是一个具有 Area1;Area2;Area3
等值的字符串。如何在 SelectMultipleField
期望的列表 ['Area1', 'Area2', 'Area3']
和我的对象期望找到的字符串之间进行转换在地区
?我有几个这样的字段,所以我不想传递类似 TestForm(areas=myObj.areas.split(';'), field2=myObj.field2.split(';'), ..., myObj)
。
I have a WTForm called TestForm
with the following property:
areas = SelectMultipleField(u'Test Areas', choices=TestArea.names())
When I create a new instance of TestForm
and pass in an object with an areas
property, the object doesn't have a list of values for areas
, but rather a string with a value like Area1;Area2;Area3
. How can I go about translating between the list ['Area1', 'Area2', 'Area3']
that the SelectMultipleField
expects, and the string that my object expects to find in areas
? I have several of these fields, so I would prefer not to have to pass in something like TestForm(areas=myObj.areas.split(';'), field2=myObj.field2.split(';'), ..., myObj)
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我现在的解决方法是在我的 SQLAlchemy 模型中进行以下设置:
然后在我的 WTForms 表单中:
My workaround for now is to have the following setup in my SQLAlchemy model:
Then in my WTForms form: