Anurag Choudhary
Anurag Choudhary

Reputation: 890

Populate the WTF Form Field value using Form inbuilt Function

Need to populate the choices of the SelectField (WTF Form) using a function. I do not want to add the choices data in the SelectField itself.

class TestForm(FlaskForm):
      dropdown = SelectField(choices=[])
      
      def form_overrided_method(self):
           self.dropdown.choices = [('A', 'A')]
    

Upvotes: 0

Views: 94

Answers (1)

yuxiaoy
yuxiaoy

Reputation: 244

You can apply values to the choices in the __init__ function of your TestForm class:

class TestForm(FlaskForm):
    dropdown = SelectField('Dropdown', coerce=int)

    def __init__(self, *args, **kwargs):
        super(TestForm, self).__init__(*args, **kwargs)
        self.dropdown.choices = [(1, 'A'),...]

Upvotes: 2

Related Questions