0

I am trying to write a function in python, that makes some introspection on one ORM object to generate a simple html form. I get the column names, like order_name , supplier_code, etc, but I would like to assign some names like "Order Name", "Supplier Code", etc.

Is there any easy way to do it, using the SQLalchemy API?

Javier Loureiro
  • 354
  • 2
  • 12

1 Answers1

1

Just get the list of column names on a table and do a simple map:

def human_readable_cols(model_cls):
    return [' '.join(part.capitalize() for part in col.split('_'))
            for col in model_cls.__table__.columns]

print human_readable_cols(MyModel)

so if the columns of the model are are ['asd_asd', 'foo', 'foo_bar'], human_readable_cols will return:

['Asd Asd', 'Foo', 'Foo Bar']

See also: How to discover table properties from SQLAlchemy mapped object

Community
  • 1
  • 1
Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111