helloflask / helloflask/bootstrap-flask

`render_table` error with `with_entities` query filter

Open
#371 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
SCSS
Stars
1.2k
Forks
193
Avg merge
4m
Merged PRs (30d)
3

Description

Hello! Thanks for this beautiful library :)

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

**vue:**

```python
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:)**

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

**error:**

```log
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.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.