简体   繁体   中英

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 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())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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