>>> x = [v for k,v in vars(Foo).items() if isinstance(v, peewee.FieldDescriptor)]
>>> for i in x:
... print(i.att_name, i.field)
...
('bar', <peewee.CharField object at 0x022AC810>)
('baz', <peewee.CharField object at 0x022AC6B0>)
('id', <peewee.PrimaryKeyField object at 0x022B4CD0>)
This leverages the Python builtin vars
generate a list of FieldDescriptor
objects that class Foo
holds. Note that we are operating on the Foo
type object directly, obtaining this information from class variables.
Once we have all the fields, we can loop over them and examine the column name and type. Most likely, you'll want some kind of isinstance
check on field
to figure out type to use in your HTML. I imagine you'll want to filter out or ignore the PrimaryKeyField
, but I think you can figure that out.
I would caution against doing this, though. It would probably be better to simply maintain the list of fields in your HTML. Yes, it's a little bit of a hassle, but automatically having new columns appear in the HTML page is not necessarily a good thing.