How to print all columns in SQLAlchemy ORM

后端 未结 10 2020
再見小時候
再見小時候 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:36

    Probably the shortest solution (see the recent documentation):

    from sqlalchemy.inspection import inspect
    columns = [column.name for column in inspect(model).c]
    

    The last line might look more readable, if rewrite it in three lines:

    table = inspect(model)
    for column in table.c:
        print column.name
    

提交回复
热议问题