Calculated Fields in web2py sqlgrid

我怕爱的太早我们不能终老 提交于 2020-01-15 15:58:23

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!