querying and selecting specific column in SQLAlchemy

后端 未结 1 743
有刺的猬
有刺的猬 2020-12-20 19:26

How do I select specific columns from a query. For example, just the User name and size of a photo from:

class User(Base):
    __tablename__ = \'user\'
    u         


        
相关标签:
1条回答
  • 2020-12-20 20:12

    Specify what you want returned in .query(). The first mapped column indicates the base table to query from.

    session.query(User.real_name, Photo.size).join(User.photos).all()
    

    This will get you a list of tuples like [('name', 12)] for every photo.

    Please consider searching the documentation briefly. It's author is very thorough.

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