I want to set attributes dynamically to a FlaskForm like this
form = ModelForm(request.form)
file form.py
class ModelForm(FlaskForm):
def __init__(self, postData):
super(ModelForm, self).__init__()
for p in postData:
setattr(ModelForm, p, StringField(p, validators=[InputRequired()]))
But it only work for the second time running, the first time running, it doesn't work.
I really don't understand how python constructor works. As this post, it said because
class A is not fully initialized when you do your setattr(A, p, v) there.
But in other languages, the object have to be created after constructor finished, and it has full class variables, properties declared in the constructor ?
For example, it works and can print a.key. So what's difference there in the flask constructor and this?
class A(object):
def __init__(self):
self.a = 'i am a accessor'
setattr(self, 'key', 'value')
a = A()
print a.a
print a.key