如何从 SQLAlchemy 结果中获取列名(声明式语法) [英] How to get column names from SQLAlchemy result (declarative syntax)

查看:35
本文介绍了如何从 SQLAlchemy 结果中获取列名(声明式语法)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个金字塔项目中工作,我在 SQLAlchemy 中有声明式语法的表

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

"""models.py"""
class Projects(Base):
    __tablename__ = 'projects'
    __table_args__ = {'autoload': True}

我通过使用得到结果

""""views.py"""
session = DBSession()
row_data = session.query(Projects).filter_by(id=1).one()

如何从这个结果中获取列名.

How can I get the column names from this result.

PS:我无法使用这个方法,因为我使用的是声明式语法.

PS: I am unable to use this method since I am using the declarative syntax.

推荐答案

ORM 和 non-ORM 的区别,不是声明式的,只是 ORM 的帮手.

The difference is between ORM and non-ORM, not declarative, which is just a helper for the ORM.

Query 有一个方法 column_descriptions() 就是为此目的添加的::

Query has a method column_descriptions() that was added for this purpose::

http://www.sqlalchemy.org/docs/orm/query.html#sqlalchemy.orm.query.Query.column_descriptions

q.columns 上的示例似乎有错别字,但它应该是 q.column_descriptions(刚刚修复它).

the example there seems like it has a typo, says q.columns but it should be q.column_descriptions (edit: just fixed it).

这篇关于如何从 SQLAlchemy 结果中获取列名(声明式语法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆