在 odoo-12 中显示组中的数字总和

Display sum of numbers in the groups in odoo-12

我创建了两个字段并在树视图中定义了它们的视图。我在字段视图中添加了属性 sum 以显示总和,但它仅在树视图中有效,在组中无效。如何在组中也显示总和?

这是 python 部分:

quantity_in = fields.Float('Quantity In', compute='get_qty_in')
quantity_out = fields.Float('Quantity Out', compute='get_qty_in')

这是字段视图的 xml 部分:

<record id="stock_move_inherit_view" model="ir.ui.view">
  <field name="name">stock.move.inherit.view</field>
  <field name="model">stock.move.line</field>
  <field name="inherit_id" ref="stock.view_move_line_tree"/>
  <field name="arch" type="xml">
    <xpath expr="//field[@name='qty_done']" position="before">
      <field name="quantity_in" sum="Total In Quantity"/>
      <field name="quantity_out" sum="Total Out Quantity"/>
      <field name="quantity_total"/>
    </xpath>
  </field>
</record>

您需要继承model/class的read_group方法: 例如你的字段名称是 quantity_in

class Your_class_name(models.Model):
    # ...        
    @api.model 
    def read_group(self, domain, fields, groupby, offset=0, limit=None, orderby=False, lazy=True):
        res = super(your_class, self).read_group(domain, fields, groupby, offset=offset, limit=limit, orderby=orderby, lazy=lazy)
        if 'quantity_in' in fields:
            for line in res:
                if '__domain' in line:
                    lines = self.search(line['__domain'])
                    in_quantity = 0.0
                    for record in lines:
                        in_quantity += record.quantity_in
                    line['quantity_in'] = in_quantity
        return res

需要存储计算字段以使总和在分组中正常工作。

quantity_in = fields.Float('Quantity In', compute='get_qty_in', store=True)