Odoo15:默认设置到期日/发票日期字段optional="hide"及树视图修改失败求助
Odoo15 发票字段optional属性设置问题解决
一、默认将到期日和发票日期的optional设为"hide"的方法
要全局默认修改这两个字段的optional属性,需分别针对表单视图和树视图做继承修改(不同视图的字段属性独立):
1. 表单视图修改
继承基础发票表单视图,直接修改字段属性:
<record id="account_move_form_inherit_hide_optional" model="ir.ui.view"> <field name="name">account.move.form.inherit.hide.optional</field> <field name="model">account.move</field> <field name="inherit_id" ref="account.view_move_form"/> <field name="arch" type="xml"> <!-- 修改发票日期 --> <xpath expr="//field[@name='invoice_date']" position="attributes"> <attribute name="optional">hide</attribute> </xpath> <!-- 修改到期日 --> <xpath expr="//field[@name='invoice_date_due']" position="attributes"> <attribute name="optional">hide</attribute> </xpath> </field> </record>
2. 树视图修改
Odoo中销项、进项发票树视图是分开的,需分别继承修改:
<!-- 销项发票树视图 --> <record id="account_out_invoice_tree_inherit_hide_optional" model="ir.ui.view"> <field name="name">account.out.invoice.tree.inherit.hide.optional</field> <field name="model">account.move</field> <field name="inherit_id" ref="account.view_out_invoice_tree"/> <field name="arch" type="xml"> <xpath expr="//field[@name='invoice_date']" position="attributes"> <attribute name="optional">hide</attribute> </xpath> <xpath expr="//field[@name='invoice_date_due']" position="attributes"> <attribute name="optional">hide</attribute> </xpath> </field> </record> <!-- 进项发票树视图 --> <record id="account_in_invoice_tree_inherit_hide_optional" model="ir.ui.view"> <field name="name">account.in.invoice.tree.inherit.hide.optional</field> <field name="model">account.move</field> <field name="inherit_id" ref="account.view_in_invoice_tree"/> <field name="arch" type="xml"> <xpath expr="//field[@name='invoice_date']" position="attributes"> <attribute name="optional">hide</attribute> </xpath> <xpath expr="//field[@name='invoice_date_due']" position="attributes"> <attribute name="optional">hide</attribute> </xpath> </field> </record>
二、你的树视图继承代码未生效的原因及修复
问题分析
你的代码存在3个核心问题:
- 模块依赖缺失:若自定义模块
__manifest__.py未添加'depends': ['account'],视图会加载失败。 - 操作方式不当:使用
replace替换整个字段标签,可能丢失原字段的必要属性(如排序、标签名),导致视图异常。 - 覆盖范围不全:仅修改了销项发票树视图,若查看的是进项发票树视图,不会生效。
修复后的代码
改用attributes直接修改属性,同时确保覆盖目标视图:
<record id="account_out_invoice_tree_inherit_hide_invoice_date" model="ir.ui.view"> <field name="name">account.move.out.invoice.tree.inherit</field> <field name="model">account.move</field> <field name="inherit_id" ref="account.view_out_invoice_tree"/> <field name="arch" type="xml"> <xpath expr="//field[@name='invoice_date']" position="attributes"> <attribute name="optional">hide</attribute> </xpath> <!-- 同步修改到期日 --> <xpath expr="//field[@name='invoice_date_due']" position="attributes"> <attribute name="optional">hide</attribute> </xpath> </field> </record>
额外检查步骤
- 升级自定义模块后,进入设置 > 技术 > 用户界面 > 视图,确认你创建的继承视图状态为
有效。 - 若存在其他修改相同视图的模块,确保你的模块在其之后安装(Odoo按安装顺序加载视图)。
- 清除浏览器缓存后刷新页面,查看效果。
内容的提问来源于stack exchange,提问作者monopoli133
相关产品推荐
相关产品推荐

