helloflask / bootstrap-flask

Bootstrap 4 & 5 helper for your Flask projects.
https://bootstrap-flask.readthedocs.io
Other
1.13k stars 191 forks source link

`render_table` error with `with_entities` query filter #371

Open Ornanovitch opened 1 month ago

Ornanovitch commented 1 month ago

Hello! Thanks for this beautiful library :)

I got an unexpected error when trying to use the table utility. Here is some context:

vue:

def board():
    profiles = models.Profile.query.filter(
        models.db.or_(
            models.Profile.status==models.Profile.REQUEST,
            models.Profile.status==models.Profile.BLOCKED,
            models.Profile.status==models.Profile.UNCLAIMED,
        )).order_by(models.Profile.status.desc())
    profiles = profiles.with_entities(models.Profile.uuid, models.Profile.username).all()
    events = models.History.query.all()
    return flask.render_template("moderation_home.html", events=events,
                                 profiles=profiles)
template (working :green_circle:)

```twig

{% for profile in profiles %} {% endfor %}
{% trans %}UUID{% endtrans %} {% trans %}username{% endtrans %}
{{ profile.uuid }} {{ profile.username }}
```

template (not working :red_circle:)

{{ render_table(profiles, primary_key='uuid') }}

error:

Traceback (most recent call last):
  File "lib/python3.12/site-packages/sqlalchemy/engine/result.py", line 199, in _key_not_found
    self._key_fallback(key, None)
  File "lib/python3.12/site-packages/sqlalchemy/engine/result.py", line 138, in _key_fallback
    raise KeyError(key) from err
KeyError: '__table__'

The above exception was the direct cause of the following exception:

  File "/dev/FELINN/hiboo/hiboo/moderation/templates/moderation_home.html", line 12, in block 'content'
    {{ render_table(profiles, primary_key='uuid') }}
  File "lib/python3.12/site-packages/jinja2/runtime.py", line 782, in _invoke
    rv = self._func(*arguments)
         ^^^^^^^^^^^^^^^^^^^^^^
  File "lib/python3.12/site-packages/flask_bootstrap/templates/base/table.html", line 48, in template
    {% set titles = get_table_titles(data, primary_key, primary_key_title) %}
  File "lib/python3.12/site-packages/flask_bootstrap/__init__.py", line 26, in get_table_titles
    for k in data[0].__table__.columns.keys():
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "lib/sqlalchemy/cyextension/resultproxy.pyx", line 66, in sqlalchemy.cyextension.resultproxy.BaseRow.__getattr__
  File "lib/sqlalchemy/cyextension/resultproxy.pyx", line 63, in sqlalchemy.cyextension.resultproxy.BaseRow._get_by_key_impl
  File "lib/python3.12/site-packages/sqlalchemy/engine/result.py", line 201, in _key_not_found
    raise AttributeError(ke.args[0]) from ke
AttributeError: __table__. Did you mean: '__le__'?

I double checked that uuid is indeed the primary key, but the result is the same with or without primary_key='uuid'.

The error appeared after I introduced

profiles = profiles.with_entities(models.Profile.uuid, models.Profile.username).all()

in my request. Without that column filter, everything works very well.