问题:Flask SQLAlchemy查询,指定列名

如何使用模型在查询中指定所需的列(默认情况下会选择所有列)?我知道如何使用sqlalchmey会话:session.query(self.col1),但是如何使用模型呢?我做不到SomeModel.query()。有办法吗?

How do I specify the column that I want in my query using a model (it selects all columns by default)? I know how to do this with the sqlalchmey session: session.query(self.col1), but how do I do it with with models? I can’t do SomeModel.query(). Is there a way?


回答 0

您可以使用该with_entities()方法来限制要返回结果的列。(文件

result = SomeModel.query.with_entities(SomeModel.col1, SomeModel.col2)

根据您的要求,您可能还会发现递延有用。它们使您可以返回完整的对象,但可以限制导线上的列。

You can use the with_entities() method to restrict which columns you’d like to return in the result. (documentation)

result = SomeModel.query.with_entities(SomeModel.col1, SomeModel.col2)

Depending on your requirements, you may also find deferreds useful. They allow you to return the full object but restrict the columns that come over the wire.


回答 1

session.query().with_entities(SomeModel.col1)

是相同的

session.query(SomeModel.col1)

对于别名,我们可以使用.label()

session.query(SomeModel.col1.label('some alias name'))
session.query().with_entities(SomeModel.col1)

is the same as

session.query(SomeModel.col1)

for alias, we can use .label()

session.query(SomeModel.col1.label('some alias name'))

回答 2

您可以使用load_only函数:

from sqlalchemy.orm import load_only

fields = ['name', 'addr', 'phone', 'url']
companies = session.query(SomeModel).options(load_only(*fields)).all()

You can use load_only function:

from sqlalchemy.orm import load_only

fields = ['name', 'addr', 'phone', 'url']
companies = session.query(SomeModel).options(load_only(*fields)).all()

回答 3

您可以使用Model.query,因为Model(通常是它的基类,尤其是在使用声明性扩展的情况下)已分配Sesssion.query_property。在这种情况下,Model.query相当于Session.query(Model)

我不知道修改查询返回的列的方法(除非使用添加更多的方法)。
因此,最好的选择是使用Session.query(Model.col1, Model.col2, ...)(如Salil所示)。

You can use Model.query, because the Model (or usually its base class, especially in cases where declarative extension is used) is assigned Sesssion.query_property. In this case the Model.query is equivalent to Session.query(Model).

I am not aware of the way to modify the columns returned by the query (except by adding more using ).
So your best shot is to use the Session.query(Model.col1, Model.col2, ...) (as already shown by Salil).


回答 4

您可以使用Query.values,Query.values

session.query(SomeModel).values('id', 'user')

You can use Query.values, Query.values

session.query(SomeModel).values('id', 'user')


回答 5

这里的一个例子:

movies = Movie.query.filter(Movie.rating != 0).order_by(desc(Movie.rating)).all()

我在数据库中查询评级为<> 0的电影,然后首先按最高评级对它们进行评级。

在这里看看:在Flask-SQLAlchemy中选择,插入,删除

An example here:

movies = Movie.query.filter(Movie.rating != 0).order_by(desc(Movie.rating)).all()

I query the db for movies with rating <> 0, and then I order them by rating with the higest rating first.

Take a look here: Select, Insert, Delete in Flask-SQLAlchemy


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。