3

I'm working on simple project and I've got a problem. I want sum one column in my one2many fields how i can do this ?

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'
    )
4

1 回答 1

3

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

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
于 2017-07-19T11:27:57.770 回答