Chaban33
Chaban33

Reputation: 1382

maximum recursion depth exceeded Odoo

class SomeOrder(models.Model):
    _inherit = 'some.order'

    @api.multi
    def write(self, vals):
        result = super(SomeOrder, self).write(vals)
        for rec in self:
            rec.update_other_order()
        return result

    @api.model
    def update_pos_quatation(self):
        # my logic.write

class OtherOrder(models.Model):
    _inherit = "other.order"

    @api.multi
    def write(self, vals):
        result = super(OtherOrder, self).write(vals)
        if 'amount_paid' in vals:
            self.update_Some_order()
        return result

    @api.model
    def update_some_order(self):
        # my logic.write

i have method that at the end writes vals to sales order but i overridden sales write method for another method to update sales order. The problem is that i get “maximum recursion depth exceeded” because update_sales_order triggers something method. how can i avoid this recursion error? probably i need to add some context to write method

So basically both methods call each other with write.

Upvotes: 1

Views: 6316

Answers (1)

CZoellner
CZoellner

Reputation: 14776

You just need a termination or break condition here.

For example, if you call another write logic if a special field value was changed, which won't change the field value again:

@api.multi
def write(self, vals):
    res = super(MyModel, self).write(vals)
    if 'my_trigger_field_name' in vals:
        self.my_other_logic(vals)

@api.multi
def my_other_logic(self, vals):
    my_trigger_field_value = vals.get('my_trigger_field_name')
    if my_trigger_field_value == 1:
        self.write({'another_field': 2})

Or if you have to change the values, which had to be written, just use to context to stop a recursion:

@api.multi
def write(self, vals):
    res = super(MyModel, self).write(vals)
    if 'stop_write_recursion' not in self.env.context:
        self.my_other_logic(vals)

@api.multi
def my_other_logic(self, vals):
    # get values to write
    self.with_context(stop_write_recursion=1).write(other_values)

Upvotes: 3

Related Questions