简体   繁体   English

如何从 SQLAlchemy ORM class 生成列值的字典?

[英]How to generate a dict of column values from an SQLAlchemy ORM class?

I have a need to serialize certain columns of several ORM based classes.我需要序列化几个基于 ORM 的类的某些列。 I want to default to the columns specified in the creation of the ORM class.我想默认创建ORM class指定的列。

class AXSection(Base):
    __tablename__ = 'axsection'
    __table_args__ = {'mysql_engine':'ISAM', 'mysql_charset':'utf8'}
    id = Column(BigInteger, primary_key=True)
    enabled = Column(String(1), nullable=False, default='T')
    pages = relationship('AXPage', backref='axsection')a
    name = Column(String(255), nullable=False)

How do I write a method to return the names of the columns?如何编写返回列名称的方法?

The table attribute contains the list of column names.属性包含列名列表。 To print the column name associated with your example AXSection type, use:要打印与您的示例 AXSection 类型关联的列名称,请使用:

print AXSection.__table__.columns

Results in:结果是:

['axsection.id', 'axsection.enabled', 'axsection.name']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQLAlchemy(ORM,声明性):如何从字典中的键/值构建查询? - SQLAlchemy (ORM, declarative): How to build query from key/values in a dict? 如何通过 python 字典更新 sqlalchemy orm object - How to update sqlalchemy orm object by a python dict SQLALchemy 查询不返回列的值(sqlalchemy.orm.exc.UnmappedInstanceError:类“builtins.str”未映射) - SQLALchemy query not returning column's values (sqlalchemy.orm.exc.UnmappedInstanceError: Class 'builtins.str' is not mapped) sqlalchemy.orm.exc.UnmappedInstanceError: Class 'builtins.dict' 未映射 - sqlalchemy.orm.exc.UnmappedInstanceError: Class 'builtins.dict' is not mapped SQLAlchemy - 热切加载子查询作为 ORM 类的列 - SQLAlchemy - Eager load subquery as column for an ORM class 当sqlalchemy ORM类属性与数据库列不同时,如何获取ORM类属性的列表? - When the sqlalchemy ORM class attributes are different from database columns, how to get a list of the ORM class attributes? 将 SqlAlchemy orm 结果转换为字典 - Convert SqlAlchemy orm result to dict SQLAlchemy ORM 集合字典 - SQLAlchemy ORM Collection Dict of Lists 在SQLAlchemy中,dict更新方法如何与ORM交互? - In SQLAlchemy, how does the dict update method interact with the ORM? 如何在SQLAlchemy Orm中将类映射到多个数据库 - How to map a Class to multiple databases in sqlalchemy orm
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM