question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sqlalchemy field names can break flask admin

See original GitHub issue

This 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:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
filmackaycommented, Jan 23, 2016

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?

0reactions
andrewmwilsoncommented, Sep 4, 2020

Does anyone have a workaround for this? Aside from renaming the field in the database?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom name of field in Flask-Admin - python - Stack Overflow
In create/edit forms I get field name like name of columns in User db.Model. How I can simply change this field name? For...
Read more >
flask_admin.model — flask-admin 1.6.0 documentation
Collection of the model field names for the list view. If set to None , will ... When using SQLAlchemy models, you can...
Read more >
Many-to-Many relationships broken on SQLAlchemy>1.3.5
Take the following model: entry_tags_table = db.Table( "entry_tags", db.Model.metadata, db.Column("entry_id", db.Integer, db.
Read more >
Flask-Admin documentation - Read the Docs
4.13 flask_admin.contrib.mongoengine.fields . ... An example is the SQLAlchemy backend, which you can use as follows:.
Read more >
SQLAlchemy in Flask — Flask Documentation (1.1.x)
You can download Flask-SQLAlchemy from PyPI. ... unique=True) email = Column(String(120), unique=True) def __init__(self, name=None, email=None): self.name ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found