Odoo - one2many 总和

Odoo - one2many sum

我正在做一个简单的项目,但遇到了问题。我想对我的 one2many 字段中的一列求和,我该怎么做?

from openerp import models, fields, api, _


class Fam(models.Model):
    _name = 'fam'

    fm_id = fields.Many2one('fam')
    mileage = fields.Float(string="Mileage", required=True)
    fueled = fields.Float(string="Fueled", required=True)
    perliter = fields.Float(string='Price per liter', required=True)


class Car2(models.Model):
    _name = 'car2'
    _description = 'Car record'
    _log_access = True

    name = fields.Char(
        string='Name',
        required=True
    )
    mile = fields.One2many(
        "fam",
        "fm_id",
        string='Mileage, Fuel and cost perliter',
        required=True
    )
    average = fields.Float(
        string='Average'
    )
    combustion = fields.Float(
        string='Combustion'
    )

您可以通过以下示例实现:

for line in self.one2many_field_name:
    total += line.field_name_in_one2many_table

# in your case
total_mileage = 0.0
total_fueled = 0.0
total_perliter = 0.0  
for line in self.mile:
    total_mileage += line.mileage
    total_fueled += line.fueled
    total_perliter += line.perliter