Sqlalchemy field names can break flask admin
See original GitHub issueThis is more of a note for future searching by me or others, though getting it into the docs would be nice.
Flask admin dynamically constructs wtforms from your sqlalchemy models. This means that your sqlalchemy fields are properties on the wtform. Sounds great! However, sqlalchemy lets you name fields anything you want, such as process
. As it happens, process
is a preexisting property in wtforms. Specifically, it’s a function used for constructing the form.
If you have a sqlalchemy model that has a process
field, it will silently override the function in wtforms. Then, during page creation, you get the following obtuse stack trace (note the process function call at the end):
Traceback (most recent call last):
File "/home/srathbun/development/google-ical-integration/env/local/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 130, in handle
self.handle_request(listener, req, client, addr)
File "/home/srathbun/development/google-ical-integration/env/local/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 171, in handle_request
respiter = self.wsgi(environ, resp.start_response)
File "/home/srathbun/development/google-ical-integration/env/local/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/home/srathbun/development/google-ical-integration/env/local/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/home/srathbun/development/google-ical-integration/env/local/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/srathbun/development/google-ical-integration/env/local/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/home/srathbun/development/google-ical-integration/env/local/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/srathbun/development/google-ical-integration/env/local/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/srathbun/development/google-ical-integration/env/local/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/home/srathbun/development/google-ical-integration/env/local/lib/python2.7/site-packages/flask_debugtoolbar/__init__.py", line 124, in dispatch_request
return view_func(**req.view_args)
File "/home/srathbun/development/google-ical-integration/env/local/lib/python2.7/site-packages/flask_admin/base.py", line 68, in inner
return self._run_view(f, *args, **kwargs)
File "/home/srathbun/development/google-ical-integration/env/local/lib/python2.7/site-packages/flask_admin/base.py", line 359, in _run_view
return fn(self, *args, **kwargs)
File "/home/srathbun/development/google-ical-integration/env/local/lib/python2.7/site-packages/flask_admin/model/base.py", line 1668, in create_view
form = self.create_form()
File "/home/srathbun/development/google-ical-integration/env/local/lib/python2.7/site-packages/flask_admin/model/base.py", line 1063, in create_form
return self._create_form_class(get_form_data(), obj=obj)
File "/home/srathbun/development/google-ical-integration/env/local/lib/python2.7/site-packages/wtforms/form.py", line 212, in __call__
return type.__call__(cls, *args, **kwargs)
File "/home/srathbun/development/google-ical-integration/env/local/lib/python2.7/site-packages/flask_admin/form/__init__.py", line 13, in __init__
super(BaseForm, self).__init__(formdata=formdata, obj=obj, prefix=prefix, **kwargs)
File "/home/srathbun/development/google-ical-integration/env/local/lib/python2.7/site-packages/wtforms/form.py", line 278, in __init__
self.process(formdata, obj, data=data, **kwargs)
TypeError: __call__() takes exactly 1 argument (4 given)
Be aware of this when constructing your sqlalchemy models, and if you run into another weird issue, try changing your field names. If the problem goes away, you have another conflict somewhere.
Issue Analytics
- State:
- Created 8 years ago
- Reactions:1
- Comments:6 (3 by maintainers)
Sounds like we need some specific warnings/errors raised when conflicting field names are encountered, or some kind of workaround to transcode them to a useable name transparently?
Does anyone have a workaround for this? Aside from renaming the field in the database?