0
votes

I'm developing a module in Odoo 10. I've two classes related by One2many-Many2one fields. I've computed fields in both classes and I want to trigger the update when values of some fields of both classes changes.

Code (very simplified)

class A(models.Model):
    _name='a'
    b_id=fields.One2many('a.b', 'a_id', ondelete='set null')

    computed_field_a = fields.Float(compute  = 'compute_vals_a', readonly = True, store = True)
    trigger_field_a = fields.Float()

    @api.multi
    @api.depends('trigger_field_a')
    def compute_vals_a(self):
        pass

class B(models.Model):
    _name='a.b'
    a_id=fields.Many2one('a', ondelete='cascade')

    compute_field_b = fields.Float(compute  = 'compute_vals_b', readonly = True, store = True)
    trigger_field_b = fields.Float()

    @api.multi
    @api.depends('trigger_field_b')
    def compute_vals_b(self):
        for r in self:
            for rr in r.a_id:
                r.compute_field_b = r.trigger_field_b * rr.trigger_field_a

What I want is that when one of trigger fields change it updates both computed fields. I'm displaying values in a form with a notebook with different pages to display A fields and B fields separately. I'm using an editable tree view to display B. What have I tried:

By now I've tried only to update fields of B by changing trigger_field_a's value

1-Change value directly from compute_vals function on class A:

    def compute_vals(self):
        b_vals=self.env['a.b'].search([('a_id', '=', self._origin.id)])
        for r in b_vals:
            r.compute_field_b = r.trigger_field_b * self.trigger_field_a

It does nothing but when I click on save I get an error: "'a' object has no attribute '_origin'"

2-Calls method from B class inside method in A class:

    def compute_vals(self):
        for r in self:
            for rr in r.a_id:
                r.compute_field_b = r.trigger_field_b * rr.trigger_field_a
        b_vals=self.env['a.b'].search([('a_id', '=', self._origin.id)])
        for r in b_vals:
            r.compute_vals_b()

I check that compute_vals_b is called (i put a _logger.error() inside and it appears in log), but computed_field_b is not updated.

3-Add trigger_field_a to @api.depends decorator of B method:

@api.depends('trigger_field_b', 'a_id.trigger_field_a')

Nothing happens

4-Call write() method from compute_vals_a. It works, but fields are updated only after saving.

5-Make my b_id field a computed field:

b_id=fields.One2many('a.b', 'a_id', ondelete='set null', compute  = 'compute_vals_a', readonly = True, store = True)

In this case, when I change trigger_field_a's value records in class B disappear from my tree view, I've checked my DB and it seems that nothing changes, I don't know why them disappear. I've seen that in this case I can't use _origin to get the id and i can get id as always with "self.id"

I've tried lots of things like create a related field from A to B to triggers the method, using super() to call the method from A... But nothing seems to work.

1

1 Answers

0
votes

change your @api.depends to @api.onchange like this:

@api.onchange('trigger_field_b', 'a_id')
def compute_vals_b(self):
    for r in self:
        for rr in r.a_id:
            r.compute_field_b = r.trigger_field_b * rr.trigger_field_a