WTForms/Flask-WTF class attributes vs instance attributes

  flask-wtforms, metaclass, python

I’m now trying to use Flask-WTF (powered by WTForms) to render my frontend forms submission.

However, there is a limitation that all the fields in a Form object are class-level attributes, namely all instances of the class share the same fields.

When I tried to add multiple form instances and render them on the same page, it just doesn’t work because the __init__ function in a Form subclass doesn’t work, you cannot add fields or modify fields in the __init__ function, and all the instances are sort of "synchronized" after I clicked the submit button.

I’m essentially trying to make a single submit button that submits several forms, while this built-in functionality of Form object hinders the process. I’m now trying to modify the BaseForm class which is inherited by Form, and see if I can dig out something to work πŸ™

I believe this has something to do with the Metaclass in WTForms, if anyone has encountered this problem please save me, and many thanks…

Source: Python Questions

LEAVE A COMMENT