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

Python 使用表单内置函数填充WTF表单字段值,python,flask,flask-wtforms,wtforms,Python,Flask,Flask Wtforms,Wtforms,需要使用函数填充SelectField(WTF表单)的选项。我不想在SelectField本身中添加选项数据 class TestForm(FlaskForm): dropdown = SelectField(choices=[]) def form_overrided_method(self): self.dropdown.choices = [('A', 'A')] 您可以将值应用于TestForm类的\uuuu ini

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

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

您可以将值应用于
TestForm
类的
\uuuu init\uuu
函数中的选项:

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

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

您可以将值应用于
TestForm
类的
\uuuu init\uuu
函数中的选项:

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

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