You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Odoo 16如何基于用户组通过XPath设置字段只读?

Odoo 16 基于用户组通过XPath设置字段只读属性的解决方案

在Odoo 16中,要通过XPath针对特定用户组修改现有字段的只读属性,不能直接沿用旧版本的groups_id继承方式,而是需要利用动态域表达式判断用户所属组,从而动态控制字段属性。针对你修改考勤模块employee_id字段的需求,具体实现如下:

实现代码

<record id="hr_attendance_form_inherit_readonly_employee" model="ir.ui.view">
    <field name="name">hr.attendance.form.inherit.readonly.employee</field>
    <field name="model">hr.attendance</field>
    <!-- 继承考勤模块的原始表单视图 -->
    <field name="inherit_id" ref="hr_attendance.view_attendance_form"/>
    <field name="arch" type="xml">
        <!-- 定位到employee_id字段 -->
        <xpath expr="//field[@name='employee_id']" position="attributes">
            <!-- 动态设置只读:仅当用户属于指定组时生效 -->
            <attribute name="readonly">user.has_group('check_in_location.group_attendance_basic')</attribute>
        </xpath>
    </field>
</record>

关键说明

  1. 动态判断逻辑:user.has_group('check_in_location.group_attendance_basic')会在视图渲染时实时检查当前用户是否属于check_in_location.group_attendance_basic组,返回True则字段设为只读,返回False则保持原有属性。
  2. 视图继承正确性:确保inherit_id引用的是考勤模块对应视图的正确外部标识符(示例中是表单视图hr_attendance.view_attendance_form,若修改树视图需替换为对应ID)。
  3. 多组组合判断:如果需要针对多个组设置只读,可通过逻辑运算符组合条件,例如:
<attribute name="readonly">
    user.has_group('check_in_location.group_attendance_basic') or user.has_group('base.group_user')
</attribute>

常见问题规避

  • 避免直接设置readonly="1",这会对所有用户生效;
  • 不要混用旧版本的groups_id继承方式,Odoo 16中该方式已不适用于动态属性控制;
  • 确认自定义组的外部标识符拼写准确,格式为模块名.组名。

内容的提问来源于stack exchange,提问作者altela

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 11:52:38