使用表单内置函数填充 WTF 表单字段值

Populate the WTF Form Field value using Form inbuilt Function

需要使用函数填充 SelectField(WTF 表单)的选项。我不想在 SelectField 本身中添加选择数据。

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

您可以将值应用于 TestForm class 的 __init__ 函数中的选项:

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

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