Dynamically query a subset of columns in sqlalchemy

前端 未结 1 343
囚心锁ツ
囚心锁ツ 2021-01-18 14:53

Assume only two columns(name and id) are needed from a table. I would code something like below:

session.query(User.id, User.name).all()

Bu

相关标签:
1条回答
  • 2021-01-18 15:37

    You can get use of select():

    from sqlalchemy.sql import select
    
    columns = ['id', 'name']
    print(session.query(select(from_obj=User, columns=columns)).all())
    

    Hope that helps.

    0 讨论(0)
提交回复
热议问题