WTForms - multi-value string to SelectMultipleField
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开发者_JAVA百科 TestForm(areas=myObj.areas.split(';'), field2=myObj.field2.split(';'), ..., myObj)
.
My workaround for now is to have the following setup in my SQLAlchemy model:
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:
areasList = SelectMultipleField(u'Test Areas', choices=TestArea.names())
精彩评论