Python – Fields are zeroed after saving

Fields are zeroed after saving… here is a solution to the problem.

Fields are zeroed after saving

I have this class in module1:

class A(models. Model):
    _name="a"
    b_id = field. Many2one("b")
    tax_old = fields. Float()
    tax_value = fields. Float(string="Tax", related = 'b_id.tax_value', store=True)
    all_taxes = fields. Float(_compute='compute_all')
    @api.depends('tax_value')
    def compute_all(self):
        self.all_taxes = self.tax_value + self.tax_old
        self.update()

In module2 I have this class:

class B(models. Model):
    _name="b"
    a_ids = fields. One2many("a","b_id")
    tax_value = fields. Float(string="Tax")

Now in A View, when I change the b_id value, tax_value works fine and compute_all works fine, but when I save this record, < em>all_taxes does not take the tax_value field, only the tax_old. When I open the record form again in View and manually write a value in tax_value, it works perfectly fine.

Solution

Using b_id on your calculation method is sufficient because it is relevant:

@api.multi
@api.depends('b_id')
def compute_all(self):
    for record in self:
        record.all_taxes = record.b_id.tax_value + record.tax_old

You can use a multi-record recordset to call a calculation method. So use a for loop inside. And you don’t have to do update() at the end.

Related Problems and Solutions