• 在OpenERP报表中使用selection 类型字段


    OpenERP 在报表的创作中始终有一个麻烦,那就是在报表中通过对象导航的方式获取的 selection 字段只能获取到该字段的 key 而不能获取对应的用户友好的描述文本。

    举个具体的例子:销售单的 state 字段,在报表中使用 [[ object.state ]] 引用时,系统返回的将是 'draft', 'manual', 'confirmed' 等内部使用的 key,而不是对应的“草稿”、“手工”和“已确认”。由于报表是供业务人员打印及浏览,所以出现系统内部代码是完全不可接受的。

    此问题 OpenERP SA 公司官方并没有提供理想的解决方案,许多系统内置的报表就直接显示了 selection 字段内部的 key。但是多亏了伟大的社区,aeroo_report 模块提供了一段非常好用的代码:

    def _get_selection_items(self, kind='items'):
        def get_selection_item(obj, field, value=None):
            try:
                if isinstance(obj, report_sxw.browse_record_list):
                    obj = obj[0]
                if isinstance(obj, (str,unicode)):
                    model = obj
                    field_val = value
                else:
                    model = obj._table_name
                    field_val = getattr(obj, field)
                if kind=='item':
                    if field_val:
                        return dict(self.pool.get(model) 
                        .fields_get(self.cr, self.uid, allfields=[field], context=self.context)
                        [field]['selection'])[field_val]
                elif kind=='items':
                    return self.pool.get(model) 
                    .fields_get(self.cr, self.uid, allfields=[field], context=self.context)
                    [field]['selection']
                return ''
            except Exception:
                return ''
        return get_selection_item

    只要把以上代码加入你的报表 parser,并在构造函数 __init__() 中将其以如下格式注册到 localcontext 中:

    'get_selection_item':self._get_selection_items('item'),
    'get_selection_items': self._get_selection_items(),

    以后在报表中调用 get_selection_item 函数即可获得 selection 字段值所对应的描述字符串:

     

    [[ get_selection_item(object, 'state') ]]
  • 相关阅读:
    shell 数组遍历加引号和不加引号的区别?
    shell map数据结构的实现
    PyCharm 项目打开窗口设置为当前还是新开一个怎么办?
    python 模拟ssh 登录远程服务器
    python 字节码死磕
    docker相关内容
    Windows7安装 docker-compose的过程
    史上最简单的Docker入门教程
    MySQL触发器使用详解
    存储过程
  • 原文地址:https://www.cnblogs.com/chjbbs/p/4895104.html
Copyright © 2020-2023  润新知