WTForms: How to select options in SelectMultipleField?

后端 未结 5 1425
抹茶落季
抹茶落季 2020-12-24 07:54

Choices can be set using form.myfield.choices=[(\"1\",\"Choice1\"), (\"2\",\"Choice2\")]

What is the way to set the selected option?

5条回答
  •  星月不相逢
    2020-12-24 08:18

    This is what worked for me (with a dynamic multi select field):

    form  = MyForm(request.form, obj=my_obj)
    form.tags.choices = [('1', 'abc'), ('2', 'def')]
    form.tags.default = ['1', '2']
    form.tags.process(request.form)
    

    If I just call form.process(), it loses the default values for the other fields in my form.

提交回复
热议问题