[英]WTForms - multi-value string to SelectMultipleField
I have a WTForm called TestForm
with the following property: 我有一个名为
TestForm
的WTForm,它具有以下属性:
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
. 当我创建的一个新实例
TestForm
并通过在与对象areas
属性,对象不具有值的列表areas
,而是与象值的字符串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
? 如何在
SelectMultipleField
期望的列表['Area1', 'Area2', 'Area3']
SelectMultipleField
['Area1', 'Area2', 'Area3']
和我的对象期望在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)
. 我有几个这样的字段,所以我不想不必传递类似
TestForm(areas=myObj.areas.split(';'), field2=myObj.field2.split(';'), ..., myObj)
。
My workaround for now is to have the following setup in my SQLAlchemy model: 我现在的解决方法是在我的SQLAlchemy模型中进行以下设置:
areas = Column(u'AREAS', VARCHAR(80))
@property
def areasList(self):
return self.areas.split(';')
@areasList.setter
def areasList(self, areas):
self.areas = ';'.join(areas)
Then in my WTForms form: 然后在我的WTForms表单中:
areasList = SelectMultipleField(u'Test Areas', choices=TestArea.names())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.