WTForms通过__init __()形式的逻辑定义许多特定字段中的一个

时间:2014-09-30 02:57:02

标签: python flask wtforms

我有一些自定义的SelectFields。我想根据一些逻辑使用其中一个。 例如:

# in view
@app.route('/some/<int:a>/', methods=['POST', 'GET'])
def f(a):
    form = MyForm(a=a)
    return render_template('t.html', form=form)

# in forms
class MyForm(Form):
    next = HiddenField()
    static_field1 = StringField()
    static_field2 = StringField()
    static_field3 = StringField()

    # this is problem
    # if a == 1 I need specific_field = CustomSelect1(),
    # but if a == 2, I need specific_field = CustomSelect()2

    specific_field = ?

我想我应该在MyForm中使用 init ()构造函数。 但我不知道如何正确地做到这一点

你能帮帮我吗?

由于

UPD 0: 也许我可以通过Meta-classes解决这个问题?

UPD 1: 我明白,存在多种方法如何做到这一点,但良好的做法是什么方式?

0 个答案:

没有答案