问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/9232336/calculated-fields-in-web2py-sqlgrid