3

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.

Tessnim
  • 434
  • 9
  • 29

3 Answers3

2

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.

CZoellner
  • 13,553
  • 3
  • 25
  • 38
1

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().

qvpham
  • 1,896
  • 9
  • 17
0

Try this instead:

# You'll need this
from django.db.models import F

@api.depends('tax_value')
def compute_all(self):
    self.update(all_taxes=F('tax_value') + F('tax_old'))

You're missing the self. What you've done is defined a local variable called all_taxes, not the instance variable.. which is what you're after

Javier Buzzi
  • 6,296
  • 36
  • 50
  • I did. Same issue. Odoo brings value from B model. The value is shown on the from view but when I save it returns back to zero. – Tessnim Mar 23 '18 at 13:51
  • 2
    @Tessnim Youre going to have to give me more code. I need to see your form, how you load it and how you save it. – Javier Buzzi Mar 23 '18 at 14:06