How do I generate dynamic fields in WTForms

前端 未结 6 2259
花落未央
花落未央 2020-12-08 16:06

I am trying to generate a form in WTForms that has dynamic fields according to this documentation http://wtforms.simplecodes.com/docs/1.0.2/specific_problems.html#dynamic-fo

6条回答
  •  情话喂你
    2020-12-08 16:31

    class BaseForm(Form):
        @classmethod
        def append_field(cls, name, field):
            setattr(cls, name, field)
            return cls
    
    from forms import TestForm
    form = TestForm.append_field("do_you_want_fries_with_that",BooleanField('fries'))(obj=db_populate_object)
    

    I use the extended class BaseForm for all my forms and have a convenient append_field function on class.

    Returns the class with the field appended, since instances (of Form fields) can't append fields.

提交回复
热议问题