Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

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

By
MouTio
on 8/30/16, 5:16 AM 189 views

In hr attendances, there is a datetime field called "name".

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 "name" editable in the "tree" view 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_attendance_tree" model="ir.ui.view">

<field name="name">hr.attendance.tree</field>

<field name="model">hr.attendance</field>

<field name="inherit_id" ref="hr_attendance.view_attendance_who"/> 

            <field name="groups_id" eval="[(6,0,[ref('base.group_hr_manager')])]" />

<field name="arch" type="xml">

<data>

                <xpath expr="//field[@name='name']" position="attributes">

<attribute name="readonly">0</attribute>

</xpath>

    </data>

    </field>

</record>  

</data>

</openerp>


That is wrong, but more or less that's the idea.

I have tried this too:

<record id="hr_attendance.name" model="ir.ui.view">

<field name="groups_id" eval="[(3,ref('base.group_hr_user'))]"/>

</record>

It doesn't work either

What I have to modify in order to achieve this?  


Note: I checked another similar post (https://www.odoo.com/es_ES/forum/ayuda-1/question/how-to-set-a-field-readonly-for-a-employee-group-85423) but it doesn't worked for me.

0
MouTio
On 9/22/16, 6:34 AM

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>


1
Divyang Jariwala
On 9/2/16, 8:07 AM

Hello MouTio,

You can do with py file by fields_view_get method.

1

Ray Carnes

--Ray Carnes--
15717
| 9 7 9
Greater Los Angeles, United States
--Ray Carnes--

Senior Odoo Analyst

OpenERP 6.1, 7.0 and Odoo 8.0, 9.0 (Since 2012)

Completed Functional and Technical Training.

Major Skills:

  • Needs Discovery and Requirements Analysis;

  • Function and Technical Specifications;

  • Project Planning;

  • Prototyping and Proof of concepts;

  • Data migration;

  • Configuration & Customization (UI and modules);

  • Integration - data, business logic and service levels;

  • Training and Knowledge transfer;

  • Go Live support;

  • Help desk;

  • Version Migration.

I have over 20 years of experience empowering and enabling users with enterprise information systems that make a real and measurable difference in their ability to proactively manage their businesses and organizations. 

Ray Carnes
On 9/2/16, 7:07 PM

You need to look at the priority of the views (sequence) and define a priority field value for yours so it isn't overridden by a higher priority view.

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 8/30/16, 5:16 AM
Seen: 189 times
Last updated: 9/22/16, 6:34 AM