Reputation: 1289
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.
Upvotes: 3
Views: 7847
Reputation: 91
If I remember correctly, there is no built-in way in Odoo to make a field editable for a certain group only.
You can make it visible or invisible by adding a group to it.
If you DO want to make the field editable depending on a group, you will need to create a new computed field that is user-dependent, and add an attrs on the field to make it readonly based on the user.
In your case, you would need something like this:
In python:
can_edit_name = fields.Boolean(compute='_compute_can_edit_name')
def _compute_can_edit_name(self):
self.can_edit_name = self.env.user.has_group('base.group_hr_user')
In your xml :
<xpath expr="//field[@name='name']" position="before">
<field name="can_edit_name" invisible="1"/>
</xpath>
<xpath expr="//field[@name='name']" position="attributes">
<attribute name="attrs">{'readonly': [('can_edit_name', '!=', True)]}</attribute>
</xpath>
This means that if the can_edit_name is True, the field will be editable.
I haven't tested it so there may be some typos, but this should give you an idea of how to do it !
Good luck !
Upvotes: 2
Reputation: 1289
I found it!
<xpath expr="//field[@name='employee_id']" position="replace">
<field name="employee_id" attrs="{'readonly':True}"/>
</xpath>
<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>
<?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>
Upvotes: 8