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

3

9

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()
Dort answered 3/5, 2011 at 11:24 Comment(0)
R
15

Can you just prepend an empty pair to the list?

form.group_id.choices.insert(0, ('', ''))
Relict answered 3/5, 2011 at 11:30 Comment(0)
M
6

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

allow_blank=True, blank_text=u'-- please choose --'
Micahmicawber answered 30/3, 2012 at 5:59 Comment(0)
P
0

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.

Potluck answered 25/1, 2021 at 18:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.