Search code examples
odooodoo-8odoo-10

odoo calculate 2 fields with compute


I have this 2 fields which is I want to calculate using compute

_columns = {
    'feb_target':fields.float('Februari Target'),
    'apr_target':fields.float('April Target'),
    'ytd':fields.float('YTD', compute='_computeytd'),
}

@api.depends('feb_target', 'apr_target')
def _computeytd(self):
    for record in self:
        record.ytd = record.feb_target + record.apr_target

But the result is 0.

What should I do? Thank you


Solution

  • Wow that took me back to my beginnings.

    Old API fields defined in _columns should use fields.function for computed fields. But i prefer to use the "new" API when possible. And your code looks like fully done by yourself, so it is possible.

    from odoo import api, fields, models
    
    class YourClass(models.Model):
    
        feb_target = fields.Float('Februari Target')
        apr_target = fields.Float('April Target')
        ytd = fields.Float('YTD', compute='_compute_ytd')
    
        @api.multi
        @api.depends('feb_target', 'apr_target')
        def _compute_ytd(self):
            for record in self:
                record.ytd = record.feb_target + record.apr_target