How to get column names from SQLAlchemy result (declarative syntax)

后端 未结 7 587
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-01 01:26

I am working in a pyramid project and I\'ve the table in SQLAlchemy in declarative syntax

\"\"\"models.py\"\"\"
class Projects(Base):
    __tablename__ = \'proje         


        
7条回答
  •  粉色の甜心
    2021-02-01 01:45

    Would like to extend @zzzeek's answer. Indeed Query has column_descriptions attribute but it's not available for all the methods.

    Consider the following two queries:

    1. query = session.query(Projects).filter_by()
    2. query = session.query(Projects).all() <-- This query does not have column_descriptions.
    

    So if you come across this situation where you need to use column_descriptions attribute but using ...query(...).all() then you can change it to ...query(...).filter_by() i.e. filter_by() without any filter condition.

提交回复
热议问题