1

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")
  • Can I put a lazy field into a grid?
  • What is the recommended way to display a grid that includes calculated fields.
David Nehme
  • 21,379
  • 8
  • 78
  • 117

1 Answers1

4

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.

Anthony
  • 25,466
  • 3
  • 28
  • 57
  • Thanks for the info Anthony. The ability to use computed fields as read-only fields is a curious omission to the .grid. I wonder if simply adding the attributes to the computed field object would make the usable or if it is more complicated. – David Nehme Feb 13 '12 at 16:33
  • +1 for the readable attribute hint. I wanted to reformat a field without duplicates, and this is the solution. – C Fairweather Apr 29 '14 at 00:00