Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm using Flask with WTForms (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()
like image 278
Daniel Ozean Avatar asked May 03 '11 11:05

Daniel Ozean


2 Answers

Can you just prepend an empty pair to the list?

form.group_id.choices.insert(0, ('', ''))
like image 166
Drew Sears Avatar answered Nov 12 '22 19:11

Drew Sears


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

allow_blank=True, blank_text=u'-- please choose --'
like image 37
moogoo Avatar answered Nov 12 '22 20:11

moogoo