. ( ) , load_only
( ), with_entities
.
>>> query = User.query
>>> query.options(load_only('email', 'id')).all()
[<User 1 using e-mail: n@d.com>, <User 2 using e-mail: n@d.org>]
>>> query.with_entities(User.email, User.id).all()
[('n@d.org', 1), ('n@d.com', 2)]
load_only
load_only()
.
. , ( ) .
" " , , , , . , :
User.query.options(load_only('name', 'fullname'))
with_entities
with_entities()
(: ) ; , , func.count()
:
query = User.query
count_query = query.with_entities(func.count(User.id)))
count = count_query.scalar()
, query.count()
, , , - MySQL ( ).
with_entities :
query = (
Page.query
.filter(<a lot of page filters>)
.join(Author).filter(<some author filters>)
)
pages = query.all()
pages_and_authors = query.with_entities(Page, Author).all()