Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

field back to zero after save

I have this class in a 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 b_id value, tax_value works fine and compute_all works fine, but when I save this record, all_taxes doesn't take tax_value field, only tax_old. And when I open the record form view again and manually write a value in tax_value, it works totally fine.

like image 535
Tessnim Avatar asked Nov 25 '25 20:11

Tessnim


2 Answers

It should be enough to use b_id on your compute method, because it's related:

@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

The compute method can be called with a multi record recordset. So use a for loop inside it. And you don't have to do an update() at the end.

like image 192
CZoellner Avatar answered Nov 27 '25 10:11

CZoellner


You can try it

    @api.one
    @api.depends('b_id', 'b_id.tax_value')
    def compute_all(self):
        self.all_taxes = self.tax_value + self.tax_old

Two things:

It ist compute not _compute and you don't need to use self.update().

like image 38
qvpham Avatar answered Nov 27 '25 10:11

qvpham



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!