如何在odoo中写出树视图的最大值?

时间:2016-06-14 04:37:39

标签: python xml openerp

我有一个树视图有3列。我想在函数字段中获得每列的最大值?

class feeder_data(osv.Model):
    _name = "feeder.data"
    _rec_name= "company_id1"
    _columns = {
        'company_id1': fields.many2one('res.company', 'Substation', required=True),
        'combine2':fields.one2many('data.value','combine','Details'),
        'max_mw': fields.function(_amount_line, string='Subtotal',type='integer'),
           }
class data_value(osv.Model):
    _name = "data.value"
    _rec_name = "mega_wat"
    _columns={
        'Hours':fields.integer('Hours'),
        'mega_wat':fields.integer('Mega Watts'),
        'combine':fields.many2one('feeder.data','details'),
    }

这里我在data.value中有树视图。在这个课程中我有mega_wat字段,我将在hourly_feeder类中输入值我有一个字段为max_wt我希望函数字段中最多包含mega_wat < / p>

2 个答案:

答案 0 :(得分:2)

首先,在模型中创建max函数:

def _get_max_of_tree(self,cr,uid,ids,context=None):
    res={}
    for o in self.browse(cr, uid, ids, context):
        res[o.id] = max(0.col1 , o.col2, o.col3)
    return res

然后为它创建一个函数字段

_columns = { 
            ...
            'mymax' : fields.function(_get_max_of_tree, type='float'),
            ...
           }

编辑: 在你的情况下,它应该是这样的:

class feeder_data(osv.Model):
    _name = "feeder.data"
    _rec_name= "company_id1"

    def _amount_line(self,cr,uid,ids,context=None):
        res={}        
        for fd in self.browse(cr, uid, ids, context):
            res[fd.id] = max([dv.mega_wat for dv in fd.combine2])
        return res

    _columns = {
        'company_id1': fields.many2one('res.company', 'Substation', required=True),
        'combine2':fields.one2many('data.value','combine','Details'),
        'max_mw': fields.function(_amount_line, string='Subtotal',type='integer'),
           }

答案 1 :(得分:-1)

使用内置最大功能。

有关模块内置

中内置函数max的帮助
max(...)
    max(iterable[, key=func]) -> value
    max(a, b, c, ...[, key=func]) -> value

    With a single iterable argument, return its largest item.
    With two or more arguments, return the largest argument.

或者您可以定义自己的功能。

def max_number(a, b, c):
    Max = a
    if b > Max:
        Max = b
    if c > Max:
        Max = c
    return Max

您的功能字段应该是这样的,

def _amount_line(self, cr, uid, ids, field_names, arg=None, context=None):
    res = {}
    for obj in self.browse(cr, uid, ids, context=context):
        max = 0
        for data in obj.combine2:
            if data.mega_wat > max:
                max = data.mega_wat

        res[obj.id] = max
    return res