sqlalchemy在应用过滤器后更改查询的对象?

时间:2012-07-16 22:40:24

标签: python sqlalchemy

说我有一个查询对象

query = session.query(SomeModel) \
    .filter(SomeModel.foo == 'bar') \
    .filter(SomeModel.active == True)

可以在执行查询之前修改对象列表,维护所有相同的过滤器

.change_query(SomeModel.id, SomeModel.name)

以便生成的查询对象为:

session.query(SomeModel.id, SomeModel.name) \
    .filter(SomeModel.foo == 'bar') \
    .filter(SomeModel.active == True)

1 个答案:

答案 0 :(得分:0)

哇已经找到了答案。如果人们正在寻找这个,请保持开放状态:

query = query.with_entities(SomeModel.id, SomeModel.name)