gorantq
gorantq

Reputation: 656

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. I want to default to the columns specified in the creation of the 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?

Upvotes: 1

Views: 193

Answers (1)

user590028
user590028

Reputation: 11730

The table attribute contains the list of column names. To print the column name associated with your example AXSection type, use:

print AXSection.__table__.columns

Results in:

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

Upvotes: 3

Related Questions