Flask, Python, Flask_WTF, set SelectField default base on DB table query?

Hello All,

I’m trying to set the default choice based on what I have set as a default in the database table. As of now I have it hard coded as “default=3” and this works but I like the default to reflect what’s in the table?

How do I reflect the default from the table to the code below.

colorSelect = SelectField('Color', coerce=str, 
choices=[(color.index, color.display) for color in dbQueryColor], default=3)

example table
Index / Name / Default
1 / Red / 0
2 / Green / 0
3 / Blue / 1
4 / Gray / 0

My python is… excessively rusty, but wouldnt it be something like… default = max(dbQueryColor, key=attrgetter('default')).index

Thank you for your assistance. It fixed the problem…

1 Like