【问题标题】:How to generate a dict of column values from an SQLAlchemy ORM class?如何从 SQLAlchemy ORM 类生成列值的字典?
【发布时间】:2012-04-20 16:53:33
【问题描述】:

我需要序列化几个基于 ORM 的类的某些列。我想默认为创建 ORM 类时指定的列。

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)

如何编写返回列名的方法?

【问题讨论】:

    标签: python orm sqlalchemy python-2.7


    【解决方案1】:

    table 属性包含列名列表。要打印与示例 AXSection 类型关联的列名,请使用:

    print AXSection.__table__.columns
    

    结果:

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

    【讨论】:

    • 答案中有错字。 AXSection.__table__.colums 应该是 AXSection.__table__.columns 无法修复它,因为 stackoverflow 需要至少 6 个字符的编辑。
    猜你喜欢
    • 2016-09-25
    • 1970-01-01
    • 2021-01-23
    • 1970-01-01
    • 2015-12-27
    • 1970-01-01
    • 2021-04-03
    • 1970-01-01
    • 2020-05-25
    相关资源
    最近更新 更多