Search code examples
odooodoo-9

How to set a field editable only for a group? In odoo9


In hr attendances, there is a field called "employee_id".

I want to set this field editable only for a group (or set readonly for other groups).

For example, I want to set the field "employee_id" editable in the "form" view only for the "manager" group.

I have extended the attendance module and I have this code in the XML of my extended module:

<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data>
    <record id="view_employee_readonly_custom" model="ir.ui.view">
        <field name="name">hr.attendance.form</field>
        <field name="model">hr.attendance</field>
        <field name="inherit_id" ref="hr_attendance.view_attendance_form"/>
        <field name="groups_id" eval="[(6,0,[ref('base.group_hr_manager')])]"/>
        <field name="arch" type="xml">
            <field name="employee_id" position="attributes">
                <attribute name="readonly">True</attribute>
            </field>
        </field>
    </record>
    </data>
</openerp>

With this code, the field is editable for everyone EXCEPT for the hr_manager group. That is the opposite that I want.

What I have to modify in order to achieve this?

Edited: I have modified the original code with a different field to better understanding.


Solution

  • I found it!

    First, the field has to be defined as readonly for everyone.

    <xpath expr="//field[@name='employee_id']" position="replace">
         <field name="employee_id" attrs="{'readonly':True}"/>
    </xpath>
    

    Then, we inherit the first custom view

    <field name="inherit_id" ref="hr_attendance_extend.view_employee_readonly_custom"/>
    

    Finally, we delete the readonly restriction for the administrators group (group_hr_manager)

    <field name="groups_id" eval="[(6, 0, [ref('base.group_hr_manager')])]"/>
    <field name="arch" type="xml">
        <xpath expr="//field[@name='employee_id']" position="attributes">
            <attribute name="readonly">False</attribute>
        </xpath>
    </field>
    

    Here is the final code:

    <?xml version="1.0" encoding="utf-8"?>
    <openerp>
        <data>
        <record id="view_employee_readonly_custom" model="ir.ui.view">
            <field name="name">hr.attendance.form</field>
            <field name="model">hr.attendance</field>
            <field name="inherit_id" ref="hr_attendance.view_attendance_form"/>
            <field name="arch" type="xml">
                <xpath expr="//field[@name='employee_id']" position="replace">
                    <field name="employee_id" attrs="{'readonly':True}"/>
                </xpath>
            </field>
        </record>
    
        <record id="view_employee_readonly" model="ir.ui.view">
            <field name="name">hr.attendance.form</field>
            <field name="model">hr.attendance</field>
            <field name="inherit_id" ref="hr_attendance_extend.view_employee_readonly_custom" />
            <field name="groups_id" eval="[(6, 0, [ref('base.group_hr_manager')])]"/>
            <field name="arch" type="xml">
                <xpath expr="//field[@name='employee_id']" position="attributes">
                    <attribute name="readonly">False</attribute>
                </xpath>
            </field>
        </record>
        </data>
    </openerp>