RecursionError: maximum recursion depth exceeded odoo 12

RecursionError: maximum recursion depth exceeded odoo 12

编辑笔记和保存时,出现错误:

 self[a[0]] = a[1] 
 RecursionError: maximum recursion depth exceeded

代码:

    notes = fields.Text('Terms', compute="_default_terms",inverse="_inverse_terms")

    @api.multi
    def _default_terms(self):
        terms = "\n\nPayment Terms:\nThis LPO is on the goods / services named and is governed by the below mentioned Terms and Conditions\n\
LPO is a must in order to start with the Job \n\
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.\n\
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.\n\
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.\n\
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.\n\
"
        self.notes = terms


    @api.multi
    def _inverse_terms(self):
        terms = "\n\nPayment Terms:\nThis LPO is on the goods / services named and is governed by the below mentioned Terms and Conditions\n\
    LPO is a must in order to start with the Job \n\
    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.\n\
    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.\n\
    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.\n\
    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.\n\
    "
        self.notes= terms

inverse函数用于逆向计算和设置相关字段。在上面的示例中,它不会反转计算,而是设置字段本身的值,这是使用反函数无法实现的。

Odoo 源代码中有很多示例,例如在 project model, is_favorite field is set to True when the current user is in project favorite_user_ids and to inverse 计算中我们检查当前用户是否是收藏用户并将其从集合中删除,如果不是则将其添加到项目收藏夹用户。

您要实现的目标与为文本字段设置默认值相同。

默认值被定义为字段的参数,可以是一个值:

notes = fields.Text('Terms', default="Payment Terms:...")

或者调用计算默认值的函数,应该return那个值:

def compute_default_value(self):
    return self.get_value()

notes = fields.Text('Terms', default=compute_default_value)

最好的方法是在公司中定义一个系统参数和一个字段,供用户稍后编辑。

以下代码添加一个参数来控制销售订单中默认付款条件的使用,以及一个相关字段来定义当前用户公司的默认付款条件。

Python代码:

class ResCompany(models.Model):
    _inherit = 'res.company'

    payment_terms = fields.Text(string='Payment Terms', translate=True)


class ResConfigSettings(models.TransientModel):
    _inherit = 'res.config.settings'

    payment_terms = fields.Text(related='company_id.payment_terms', string="Terms", readonly=False)
    use_payment_terms = fields.Boolean(
        string='Payment Terms',
        config_parameter='sale.use_payment_terms')


class SaleOrder(models.Model):
    _inherit = 'sale.order'

    @api.model
    def compute_default_value(self):
        return self.env['ir.config_parameter'].sudo().get_param('sale.use_payment_terms') and self.env.user.company_id.payment_terms or ''

    notes = fields.Text('Terms', default=compute_default_value)

XML:(在Quotations & Orders配置中添加付款条件)

<record id="res_config_settings_view_form" model="ir.ui.view">
    <field name="name">res.config.settings.view.form.inherit.sale</field>
    <field name="model">res.config.settings</field>
    <field name="inherit_id" ref="base.res_config_settings_view_form"/>
    <field name="arch" type="xml">
        <xpath expr="//div[@id='sale_config_online_confirmation_pay']/parent::div" position="inside">
            <div class="col-6 col-lg-6 o_setting_box">
                <div class="o_setting_left_pane">
                    <field name="use_payment_terms"/>
                </div>
                <div class="o_setting_right_pane">
                    <label for="use_payment_terms"/>
                    <span class="fa fa-lg fa-building-o" title="Values set here are company-specific." aria-label="Values set here are company-specific."
                          groups="base.group_multi_company" role="img"/>
                    <div class="text-muted">
                        Show payment terms on orders
                    </div>
                    <div class="content-group" attrs="{'invisible': [('use_payment_terms','=',False)]}">
                        <div class="mt16">
                            <field name="payment_terms" placeholder="Insert your terms here..."/>
                        </div>
                    </div>
                </div>
            </div>
        </xpath>
    </field>
</record>  

如果要使用逆向方法更改计算域的值,计算域应根据持久且可变的值进行计算。

否则,您会得到您现在看到的 effect/error:您的字段是根据代码中的字符串计算的。所以逆向方法应该改变那个字符串的值,这是不可能的(或者非常非常困难)。

只需定义一个默认值或默认方法,如 Kenly 的回答:

@api.model
def _default_terms(self):
    return "terms..."

notes = fields.Text('Terms', default=_default_terms)

另一个好方法是在公司上定义系统参数或可配置字段,可以在设置页面上配置,就像 Odoo 中的许多其他内容一样。

然后您可以从该参数或公司字段中获取默认值作为默认值。这样,默认值在 Odoo 本身是可变的。