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
我的方法是在最后将vals写入销售订单但我覆盖了销售写入方法,以便更新销售订单。问题是我得到“超出最大递归深度”,因为update_sales_order触发了一些方法。我怎样才能避免这种递归错误?可能我需要添加一些上下文来编写方法
所以基本上两种方法都是用write来互相调用的。
答案 0 :(得分:1)
这里只需要终止或中断条件。
例如,如果更改了特殊字段值,则调用另一个写入逻辑,这不会再次更改字段值:
@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})
或者,如果您必须更改必须写入的值,只需使用上下文来停止递归:
@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)