本文介绍了web2py sqlgrid 中的计算字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Web2py 有几种计算字段的方法,但文档指出惰性字段默认情况下不会在表格中可视化",因为它们没有像 _ 这样的属性.事实上,即使请求该字段,它们似乎也无法在 SQLFORM.grid 中使用.我收到错误

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")

  • 我可以将惰性字段放入网格中吗?
  • 显示包含计算字段的网格的推荐方式是什么.
  • 推荐答案

    不幸的是,我认为没有一种简单的方法可以在 SQLFORM.grid 中显示虚拟字段.您可以做的是使用links"参数并将每个虚拟字段添加为链接(如果links"是字典,则每个项目将成为网格中的单独列).

    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)]
    

    注意,在这种情况下,您不能指定fields"参数(即,您不能仅指定包含在网格中的字段子集)——这是因为虚拟字段函数需要按顺序排列所有字段上班.如果您需要隐藏某些字段,您可以将它们的可读"属性设置为 False.

    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.

    另一个选项可能是计算字段.

    这篇关于web2py sqlgrid 中的计算字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 15:02