I have another quick question about SQLAlchemy.
If I have added a query_property
[1] field in my SQLAlchemy Table class, is it possible to narrow down the SELECTed fields?
Here is what I mean.
Suppose my class Comments
has this:
class Comments:
query = Session.query_property()
...
Then if I do the following:
>>> print Session.query(Comment)
Then SQLAlchemy generates the following query which includes ALL the columns in my comments
table:
SELECT comments.comment_id AS comments_comment_id,
comments.comment AS comments_comment
... more columns ...
FROM comments
But I want to narrow down this query and select only (let's say) the comment
field as in the following construct:
>>> print Session.query(Comment.comment)
SELECT comments.comment AS comments_comment
FROM comments
Is it possible to do this via Comment.query
construct?
I tried the following but it didn't work:
>>> print Comment.query(Comment.comment)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'Query' object is not callable
Please help me with an advice.
Thanks, Boda Cydo.
[开发者_如何学运维1] http://www.sqlalchemy.org/docs/reference/orm/sessions.html#sqlalchemy.orm.scoping.ScopedSession.query_property
Try using query.values(Comment.comment)
. Note that it returns generator, not modified query, so this method should called last after applying all filters.
精彩评论