[英]Adding related column to query results?
I have a setup in SQLAlchemy ORM (using Flask-SQLAlchemy) with Book
and BookSubject
classes, the latter being a many-to-many relationship (there is, of course, a Subject
class, but it's not relevant to this question). I have a setup in SQLAlchemy ORM (using Flask-SQLAlchemy) with Book
and BookSubject
classes, the latter being a many-to-many relationship (there is, of course, a Subject
class, but it's not relevant to this question). I have a working query to return all books based on the date the subject was added to the database (there's a reason for this):我有一个工作查询,可根据主题添加到数据库的日期返回所有书籍(这是有原因的):
records = Book.query.\
filter(BookSubject.book_id == Book.id).\
filter(BookSubject.subject_id.in_([1, 12, 17])).\
filter(BookSubject.created > '2021-01-01').\
order_by(db.desc(BookSubject.created)).\
paginate(page, 50, True)
I then pass records
to a Jinja2 template and do assorted stuff to display it;然后我将records
传递给 Jinja2 模板并做各种事情来显示它; it works perfectly.它完美地工作。
I'd now like to do the obvious thing and actually display the creation date (ie BookSubject.created
, from the order_by clause), but I can't figure out how to add this to the query.我现在想做显而易见的事情并实际显示创建日期(即BookSubject.created
,来自 order_by 子句),但我不知道如何将其添加到查询中。 Putting in add_columns((BookSubject.created).label("added"))
isn't the answer;放入add_columns((BookSubject.created).label("added"))
不是答案; that throws an error when I try to use one of the record objects "'sqlalchemy.util._collections.result object' has no attribute 'id'".当我尝试使用其中一个记录对象“'sqlalchemy.util._collections.result object'没有属性'id'”时会引发错误。 The template code that generates this is (roughly):生成它的模板代码(大致)是:
{% for book in records.items %}
<tr><td><a href="{{ url_for('fullview', id=book.id) }}">{{ book.title }}</a></td></tr>
{% endfor %}
This should be obvious;这应该是显而易见的; how am I meant to add this to the result?我如何将其添加到结果中?
By using add_columns
通过使用add_columns
Book.query.add_columns((BookSubject.created).label("added"))
it will return a named tuple with fields Book
and added
, so to access book fields you'd need something like book.Book.id
它将返回一个带有字段Book
和added
的命名元组,因此要访问 book 字段,您需要类似book.Book.id
{% for book in records.items %}
<tr><td>
<a href="{{ url_for('fullview', id=book.Book.id) }}">{{ book.Book.title }} {{ book.added }} </a>
</td></tr>
{% endfor %}
or iterate by pairs:或成对迭代:
{% for book, added in records.items %}
<tr><td>
<a href="{{ url_for('fullview', id=book.id) }}">{{ book.title }} {{ added }} </a>
</td></tr>
{% endfor %}
If you want a flat structure (and without add_columns
), then you can use如果您想要一个平面结构(并且没有add_columns
),那么您可以使用
session.query(
Book.id,
Book.title,
BookSubject.created.label('added')
).filter(BookSubject.book_id == Book.id)...
then results will have a named tuple with fields id
, title
and added
, so you can print directly book.id
:然后 results 将有一个包含字段id
、 title
和added
的命名元组,因此您可以直接打印book.id
:
{% for book in records.items %}
<tr><td>
<a href="{{ url_for('fullview', id=book.id) }}">{{ book.title }} {{ book.added }} </a>
</td></tr>
{% endfor %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.