User
User

Reputation: 66071

How do I get list of field objects in a table in web2py?

How do I get a list of all the field objects (e.g. gluon.dal.Field) in a table? The following

db.customer.fields

just returns a list of strings which are the field names.

Upvotes: 4

Views: 2265

Answers (2)

Anthony
Anthony

Reputation: 25536

field_objects = [f for f in db.customer]

Upvotes: 6

User
User

Reputation: 66071

Okay I see that fields are defined as attributes of the table class (gluon.dal.Table). The table class has a __getitem__ method defined which allows indexing by attribute name (as python allows).

Therefore I can get a list of field objects by using a list comprehension:

[db.customer[fieldname] for fieldname in db.customer.fields]

Upvotes: 1

Related Questions