开发者

Wtforms: How to generate blank value using select fields with dynamic choice values

I'm using Flask with WTFo开发者_如何学Pythonrms (doc) on Google App Engine. What is the best way to generate an field with an empty value for a select field?

form.group_id.choices = [(g.key().id(), g.name) for g in Group.all().order('name')]

Is there something like "blank=True" for the form field?

myfield = wtf.SelectField()


Can you just prepend an empty pair to the list?

form.group_id.choices.insert(0, ('', ''))


If it's a QuerySelectField, you can add parameters like this:

allow_blank=True, blank_text=u'-- please choose --'


I prefer such a way:

form.group_id.choices = ['none'] + [(g.key().id(), g.name) for g in Group.all().order('name')]

and that's enough - you don't need to deal with 'none' value.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜