贝利信息

如何在 Odoo 中基于字段值动态控制表单字段的可见性

日期:2026-01-11 00:00 / 作者:心靈之曲

本文详解如何在 odoo 中通过视图属性 `attrs` 实现条件显示字段——以“仅当员工子女数量 > 0 时才显示子女姓名与出生日期字段”为例,提供简洁、高效、符合 odoo 最佳实践的解决方案。

在 Odoo 开发中,常见的业务需求是根据某字段的值(如整数字段 children)动态控制其他字段的可见性。题中目标明确:新增两个字段 name_child(子女姓名)和 date_of_birth(出生日期),仅当 children > 0 时才在表单中显示;且需置于「Private Information」标签页中、位于原 children 字段下方。

然而,原代码存在多个关键问题:

✅ 正确解法分两步:

1. 模型层:正确定义一对多关系(推荐)
若需真正支持“多个子女”,应创建独立的 hr.employee.child 模型,并通过 One2many 关联:

# models/hr_employee.py
class HrEmployee(models.Model):
    _inherit = 'hr.employee'

    children = fields.Integer(string='Number of Children')
    child_ids = fields.One2many(
        'hr.employee.child', 'employee_id',
        string='Children Details'
    )

class HrEmployeeChild(models.Model):
    _name = 'hr.employee.child'
    _description = 'Employee Child Information'

    employee_id = fields.Many2one('hr.employee', required=True, ondelete='cascade')
    name = fields.Char(string='First and Last Name', required=True)
    date_of_birth = fields.Date(string='Date of Birth')

2. 视图层:用 attrs 实现条件可见性(针对单字段场景或 One2many widget)

即使采用 One2many 方式,其列表控件本身默认可见;若需控制整个 child_ids 字段块的显示,可在视图中添加:



⚠️ 注意事项:

? 总结:Odoo 的 UI 动态行为首选声明式 attrs,而非命令式 onchange 赋值;而真实业务中“多个子女”必须通过 One2many 建模,确保数据完整性与扩展性。