Odoo 11:在循环中调用 Write 方法

Odoo 11 : Write method is called in loop

这是我的代码:

@api.multi
def _upgrading_balance(self):
    .........
    self.write({
        'balance_initial': new_balance_initial
    })
    .........
@api.multi
def write(self, vals):
    result = super(InheritedHoranetPackageLine, self).write(vals)
    if vals.get('balance_initial'):
        self._upgrading_balance()

    return result

我的写入方法是循环调用的。如何拨打电话一次?

谢谢

一个非常简单但可能不是完美的解决方案是使用上下文来决定是否升级余额:

@api.multi
def _upgrading_balance(self):
    # things
    self.write({
        'balance_initial': new_balance_initial
    })
    # more things

@api.multi
def write(self, vals):
    result = super(InheritedHoranetPackageLine, self).write(vals)
    if vals.get('balance_initial') and\
            'balance_upgrade' not in self.env.context:
        self.with_context(balance_upgrade=1)._upgrading_balance()
    return result