How could I get the list of a Model's columns (and later of id columns, and relationships) as:
columns = Model.cols_list
so that the list would be constructed one time and reused when needed.
I would have writen something like this, that I can't get to work :
from sqlalchemy.orm import class_mapper
class Model(DeclarativeBase):
__tablename__ = 'model_table'
id = Column(Integer, primary_key=True, autoincrement=True)
col1 = Column(...)
col2 = Column(...)
...
cols_list = None
@classmethod
def cols_list(cls):
if cls.cols_list is None:
cls.cols_list = [key.name for key in class_mapper(cls).primary_key]
return cls.cols_list
Model.__table__.columns?