How to print all columns in SQLAlchemy ORM

后端 未结 10 2027
再見小時候
再見小時候 2020-12-28 15:18

Using SQLAlchemy, I am trying to print out all of the attributes of each model that I have in a manner similar to:

SELECT * from table;

How

10条回答
  •  清歌不尽
    2020-12-28 15:44

    This is an old post, but I ran into a problem with the actual database column names not matching the mapped attribute names on the instance. We ended up going with this:

    from sqlalchemy import inspect
    inst = inspect(model)
    attr_names = [c_attr.key for c_attr in inst.mapper.column_attrs]
    

    Hope that helps somebody with the same problem!

提交回复
热议问题