I'm working on an app that uses WTForms in combination with an templating engine.
The forms that are being shown to the user are not pre defined, but can be dynamically configured by the admin. So the WTForm needs to be able to dynamically add fields, I have achieved this by specifying this subclass of Form (as suggested by):
class DynamicForm(Form):
@classmethod
def append_field(cls, name, field):
setattr(cls, name, field)
return cls
This works fine, but it seems that via this method you cannot populate the fields through Form(obj=values_here). This is my current instantiation of the Form subclass mentioned above:
values = {}
values['password'] = 'Password123'
form = DynamicForm(obj=values)
form.append_field("password", PasswordField('password'))
So this won't work, and this makes sense when you consider the field to be added after the Form's init has been called. So I have been searching for a way to also assign the value of a field dynamically, but I haven't had any luck so far.
Does anyone know of a way to set the password field in the example to a certain value? Would be greatly appreciated.