Odoo 12编辑保存notes字段时触发RecursionError:超出最大递归深度错误
错误原因
- 触发逻辑形成死循环:Odoo中绑定了
compute和inverse的计算字段,对字段本身赋值会触发对应方法。你在_default_terms(compute方法)中执行self.notes = terms时,会触发该字段的inverse方法_inverse_terms;而_inverse_terms中又执行self.notes = terms,会再次触发compute方法,无限循环最终导致递归深度超限。 - 方法用途误用:
compute方法的作用是根据依赖字段动态计算当前字段值,inverse方法的作用是将计算字段的赋值反向写入关联的持久化字段,你两个方法都写死固定字符串,完全不符合设计逻辑,如果你只是需要字段默认值,不需要使用计算字段。
修复方案
根据你的实际需求选择对应方案:
方案1:仅需要固定默认值(推荐)
直接去掉compute和inverse参数,改用default设置默认值即可,代码如下:
notes = fields.Text('Terms', default=""" Payment Terms: This LPO is on the goods / services named and is governed by the below mentioned Terms and Conditions LPO is a must in order to start with the Job If the supplier fails to deliver on time and thereby resulting on the rejection or cancellation of job, MAI BLUE has the right to cancel the job and has no financial obligation towards the supplier, and if MN BLUE has already paid any down payment; the supplier must return it to the company immediately. MAI BLUE can demand an amendment / modification due to sub-standard / low quality products / services provided, at supplier's cost. if the supplier is unable to rectify the product before final delivery MAI BLUE has the right not to pay the balance amount and demand back any advance payments made for the job. Delays in delivery and/or delivering the goods not as per agreed quality / specifications will lead to job cancellation and/or 50% reduction in payment from MAI BLUE towards the supplier. Once the supplier submits their final Quotation, no changes in materials or specification will be accepted after the supplier confirms or submit his quotation. Any changes will be under the supplier's responsibility/cost. """)
方案2:确实需要使用可编辑计算字段
新增一个持久化字段存储用户输入的内容,compute和inverse方法均操作该存储字段,避免直接操作当前计算字段触发循环:
# 新增持久化存储字段 notes_store = fields.Text() # 计算字段绑定依赖和反向方法 notes = fields.Text('Terms', compute="_compute_terms", inverse="_inverse_terms") @api.depends('notes_store') def _compute_terms(self): default_terms = """你原来的默认条款内容""" for record in self: record.notes = record.notes_store if record.notes_store else default_terms def _inverse_terms(self): for record in self: # 把用户输入的内容写入存储字段,不操作notes本身 record.notes_store = record.notes
内容的提问来源于stack exchange,提问作者Shahal
相关产品推荐
相关产品推荐

