David Nehme
David Nehme

Reputation: 21597

Calculated Fields in web2py sqlgrid

Web2py has several methods for calculated fields, but the documentation states that lazy fields "are not visualized by default in tables" because they don't come with attributes like _. In fact, they don't seem to be able to be available in SQLFORM.grid even if the field is requested. I get the error

AttributeError: 'FieldLazy' object has no attribute 'readable'

When I include a lazy field in the field list.

db.mytable.myfield = Field.Lazy(lambda row: "calc")

Upvotes: 1

Views: 2513

Answers (1)

Anthony
Anthony

Reputation: 25536

Unfortunately, I don't think there is an easy way to display virtual fields in SQLFORM.grid. What you can do is use the "links" argument and add each virtual field as a link (if "links" is a dictionary, each item will become a separate column in the grid).

links=[dict(header='myfield', body=lambda row: row.myfield)]

Note, in this case, you cannot specify the "fields" argument (i.e., you cannot specify only a subset of the fields for inclusion in the grid) -- this is because the virtual field function needs all the fields in order to work. If you need to hide some of the fields, you can instead set their "readable" attibute to False.

Another option might be computed fields.

Upvotes: 4

Related Questions