Closed vincentwhales closed 7 years ago
This is an issue of how you've setup your model relationships. It doesn't have anything to do with WTForms-Alchemy. See SA documentation of how to set-up backref relationships.
Actually I beg to differ.
File "/Users/vng/.virtualenvs/AutomataHeroku/lib/python2.7/site-packages/wtforms_alchemy/__init__.py", line 116, in __init__
generator.create_form(cls)
File "/Users/vng/.virtualenvs/AutomataHeroku/lib/python2.7/site-packages/wtforms_alchemy/generator.py", line 142, in create_form
for key, property_ in sa.inspect(self.model_class).attrs.items():
I believe this error is caused by WTForms-Alchemy generating the ModelForm which invokes this line, for key, property_ in sa.inspect(self.model_class).attrs.items()
, before the model (Account in this case) is defined.
This is happening because the creation of a ModelForm involves inspecting the attributes of that form. Some of these attributes are relationship attributes defined by backref/back_populates. These relationships reference another model that is not yet defined.
Is there anyway to fix this?
Perhaps you could lazily generate your forms using factory or by placing the form in a separate file and making sure models are configured before the form definition (by calling for example sqlalchemy.orm.configure_mappers()
)
I have two models: Cloaker and Account. Note that I have simplified both classes for the sake of this question.
automata/cloaker/models.py
automata/account/models.py
When I run flask, I am getting the following error:
What is causing this error?