如何解决Odoo16中点击「其他信息」标签触发的Owl生命周期错误?
问题:禁用销售订单表单创建/编辑后切换标签报错
我通过继承表单视图来禁用sale.order表单的创建、编辑功能,代码如下:
sale.order_view.xml
<record id="view_order_form_inherit_custom" model="ir.ui.view"> <field name="name">sale.order.inherit</field> <field name="model">sale.order</field> <field name="inherit_id" ref="sale.view_order_form"/> <field name="arch" type="xml"> <form position="attributes"> <attribute name="edit">false</attribute> <attribute name="create">false</attribute> </form> </field> </record>
这段代码可正常禁用销售订单视图的创建和编辑功能,但点击「其他信息」标签时会出现以下错误,注释该代码后错误消失,求解决办法。
错误信息:
UncaughtPromiseError > OwlError Uncaught Promise > An error occured in the owl lifecycle (see this Error's "cause" property) OwlError: An error occured in the owl lifecycle (see this Error's "cause" property) OwlError@http://localhost:5014/web/assets/debug/web.assets_common.js:16481:5 (/web/static/lib/owl/owl.js:87) handleError@http://localhost:5014/web/assets/debug/web.assets_common.js:16518:35 (/web/static/lib/owl/owl.js:124) handleError@http://localhost:5014/web/assets/debug/web.assets_common.js:22093:20 (/web/static/lib/owl/owl.js:5699) _render@http://localhost:5014/web/assets/debug/web.assets_common.js:18013:30 (/web/static/lib/owl/owl.js:1619) render@http://localhost:5014/web/assets/debug/web.assets_common.js:18002:18 (/web/static/lib/owl/owl.js:1608) initiateRender@http://localhost:5014/web/assets/debug/web.assets_common.js:18741:23 (/web/static/lib/owl/owl.js:2347) Caused by: TypeError: this.options.find(...) is undefined get string@http://localhost:5014/web/assets/debug/web.assets_backend.js:37198:36 (/web/static/src/views/fields/selection/selection_field.js:28) template@http://localhost:5014/web/assets/debug/web.assets_common.js line 21874 > Function:15:18 (/web/static/lib/owl/owl.js:5480) _render@http://localhost:5014/web/assets/debug/web.assets_common.js:18010:38 (/web/static/lib/owl/owl.js:1616) render@http://localhost:5014/web/assets/debug/web.assets_common.js:18002:18 (/web/static/lib/owl/owl.js:1608) initiateRender@http://localhost:5014/web/assets/debug/web.assets_common.js:18741:23 (/web/static/lib/owl/owl.js:2347)
解决方案
问题根源
直接给整个表单设置edit="false"会破坏Odoo的视图渲染逻辑:只读模式下部分字段(如选择型字段)需要依赖可编辑模式的上下文加载选项列表,强制全局只读会导致这些字段无法获取必要的选项数据,从而触发this.options.find(...) is undefined错误。
可行解决方式
1. 权限控制(推荐)
通过用户组权限限制sale.order模型的创建和编辑权限:
- 进入设置 -> 权限 -> 权限组
- 找到目标用户组,在「对象权限」中定位
sale.order模型 - 取消勾选「创建」和「编辑」权限
这种方式符合Odoo的权限体系,不会引发视图渲染问题。
2. 字段级只读设置
如果必须通过视图控制,不要全局禁用edit,而是给每个字段单独设置只读属性,同时禁用创建功能:
<record id="view_order_form_inherit_custom" model="ir.ui.view"> <field name="name">sale.order.inherit</field> <field name="model">sale.order</field> <field name="inherit_id" ref="sale.view_order_form"/> <field name="arch" type="xml"> <!-- 禁用创建功能 --> <form position="attributes"> <attribute name="create">false</attribute> </form> <!-- 给核心字段设置只读 --> <field name="partner_id" position="attributes"> <attribute name="readonly">1</attribute> </field> <field name="pricelist_id" position="attributes"> <attribute name="readonly">1</attribute> </field> <field name="order_line" position="attributes"> <attribute name="readonly">1</attribute> </field> <!-- 按需添加其他需要设置只读的字段 --> </field> </record>
3. 使用readonly_mode属性(Odoo 14+)
Odoo 14及以上版本提供了readonly_mode属性,可安全强制表单进入只读模式,兼容所有字段渲染:
<record id="view_order_form_inherit_custom" model="ir.ui.view"> <field name="name">sale.order.inherit</field> <field name="model">sale.order</field> <field name="inherit_id" ref="sale.view_order_form"/> <field name="arch" type="xml"> <form position="attributes"> <attribute name="create">false</attribute> <attribute name="readonly_mode">true</attribute> </form> </field> </record>
内容的提问来源于stack exchange,提问作者KbiR
相关产品推荐
相关产品推荐

