开发者

WTForms - multi-value string to SelectMultipleField

开发者 https://www.devze.com 2023-03-14 15:11 出处:网络
I have a WTForm called TestForm with the following property: areas = SelectMultipleField(u\'Test Areas\', choices=TestArea.names())

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号