按组筛选时计算平均值

2024-04-19 17:00:53 发布

您现在位置:Python中文网/ 问答频道 /正文

当我试图在树视图中显示字段的平均值时,我遇到了一个问题。在

percentage = fields.Float(string='Porcentaje', compute='fill_percentage', store=True,default=False)

  @api.model
  def read_group(self, domain, fields, groupby, offset=0, limit=None, orderby=False, lazy=True):
    res = super(Project_Task_Extension, self).read_group(domain, fields, groupby, offset=offset,
                                                         limit=limit, orderby=orderby, lazy=lazy)

    if 'percentage' in fields:
        for line in res:
            if '__domain' in line:
                value = 0.0
                aux = 0
                lines = self.search(line['__domain'])
                for x in lines:
                    value += x.percentage
                    aux += 1
                total = value/aux
                line['percentage'] = total
    return res

    ----in the xml----
    <field name="percentage" avg="Average"/>

这段代码工作得很好,但是当我试图用“project”进行过滤时,最终的平均值是不正确的,有什么建议吗?在

In this image the average is correct

but with this filter the total average is wrong

在第二张图片中,总数应该是(85.71+100+0)/3=61.90。。。在


Tags: theinselffieldsvaluedomainlineres