Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WTForms: How to select options in SelectMultipleField?

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

What is the way to set the selected option?

like image 946
lecodesportif Avatar asked Apr 01 '11 22:04

lecodesportif


People also ask

What does WTForms stand for?

WTF stands for WT Forms which is intended to provide the interactive user interface for the user. The WTF is a built-in module of the flask which provides an alternative way of designing forms in the flask web applications.

What is Stringfield?

The string field is used for allowing users to enter unformatted text information into your form in a limited fashion. Creating multiple strings is useful for collecting directed information, such as answers from your user requiring single words or brief sentences.

What is WTForms flask?

WTForms is a Python library that provides flexible web form rendering. You can use it to render text fields, text areas, password fields, radio buttons, and others. WTForms also provides powerful data validation using different validators, which validate that the data the user submits meets certain criteria you define.


3 Answers

You can use the choices and default keyword arguments when creating the field, like this:

my_choices = [('1', 'Choice1'), ('2', 'Choice2'), ('3', 'Choice3')]

SelectMultipleField(choices = my_choices, default = ['1', '3'])

This will mark choices 1 and 3 as selected.


Edit: Default values are apparently processed (copied into the data member) when the form is instatiated, so changing the default afterwards won't have any effect, unless you manually call process() on the field. You could set the data -member, like so:

form.myfield.data = ['1', '3']

But I'm not sure if either of them is a good practice.


Edit: In case you want to actually set the data and not the default, you should probably use the form to load the data.

Form objects take formdata as the first argument and use that to automatically populate field values. (You are supposed to use a dictionary wrapper with a getlist -method for that)

You can also use keyword arguments to set the data when creating the form, like this:

form = MyForm(myfield = ['1', '3'])
like image 82
Aleksi Torhamo Avatar answered Oct 19 '22 09:10

Aleksi Torhamo


This is what worked for me on a SelectField:

form.myfield.default = '1'
form.process()

I'm guessing you can just assign a list to form.myfield.default for a SelectMultipleField. The key, though, seems to be calling the process method on the form after you assign to default.

like image 19
krupan Avatar answered Oct 19 '22 11:10

krupan


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.

like image 5
Jaza Avatar answered Oct 19 '22 09:10

Jaza