Open clee704 opened 10 years ago
I agree better support for app-factory-based Flask apps are not as easily supported as the documentation claims. However, in case it helps others, here's a much easier approach that I have working in my own app that is far less ugly (though still disappointing)
# in project/forms/__init__.py
from flask_wtf import Form
from wtforms_alchemy import model_form_factory
from project.models import db
BaseModelForm = model_form_factory(Form)
class ModelForm(BaseModelForm):
@classmethod
def get_session(self):
return db.session
# in another or same file, shouldn't matter
# if in another file, say project/forms/foo.py,
# just remember to import ModelForm
from project.models import MyModel
class MyModelForm(ModelForm):
class Meta:
model = MyModel
# other meta options
Everything else works as desired, as far as I can see.
+1
Does this include the CSRF token?
model_form_factory(Form)
produces a metaclass conflict using wtforms_alchemy 0.15.0 with flask_wtforms 0.12 and wtforms 2.1 – which version would you need for this to work?
Given these instructions... https://wtforms-alchemy.readthedocs.io/en/latest/advanced.html#using-wtforms-alchemy-with-flask-wtf Has this issue been effectively solved now?
When you do this:
You might see the following error message:
If you are following the app factory pattern.
The solution is doing some hackish things:
and setting
model_deferred
instead ofmodel
onMeta
. I think there should be some way to do this more easily, because if you have a non-trivial Flask app then the app factory pattern is a must.