How to dynamically set default value in WTForms RadioField?
You need to run myForm.process()
after adding the choices and setting the default
property:
myForm = MyForm()myForm.display.choices = [('ONE', 'one'), ('TWO', 'two')]myForm.display.default = 'ONE'myForm.process() # process choices & default
This is because the default
is propagated to the field value (and, in the case of RadioField
, the checked
property) in the process
method, which is called in the constructor.