This question has been flagged
2 Replies
20486 Views

Hello,

I have a field named "field1",  that field should be not editable by user who has group named "Bloc fields" checked.

My test code is below:

 1/file.xml

 <record id="res_partner_form_update" model="ir.ui.view">

    <field name="name">res.partner.form.update</field>

    <field name="model">res.partner</field>

    <field name="inherit_id" ref="base.view_partner_form"/>

    <field name="groups_id" eval="[(4,ref('custom_module.bloc_fields'))]"/>

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

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

        <field name="field1"/>

    </xpath>

    </field>

</record>


2/security.xml

 <record id="bloc_fields" model="res.groups">

    <field name="name">Bloc fields</field>

    <field name="comment">Enable/Disable fields</field> 

</record>


3/ir.model.access.csv

"id","name","model_id:id","group_id:id","perm_read","perm_write","perm_create","perm_unlink"

"access_res_partner_bloc_fields","res_partner bloc_fields","base.model_res_partner","bloc_fields",1,0,0,0



Result:

the field "field1" is gone, it is no longer visible but when i comment  

<!-- <field name="groups_id" eval="[(4,ref('custom_module.bloc_fields'))]"/> --> it will be displayed

Avatar
Discard
Best Answer

The idea is create a computed field that checks login user gorup. And we need to visible invisible some filed on the basis of login user group or we want to perform some action on the basis of login user.

How to check login user group: http://learnopenerp.blogspot.com/2017/10/how-to-check-login-user-group-in-odoo.html

After creating computed field, use that field in your xml (to make field read-only)

<field name="your_computed_field_that_check_login_user_group" attrs="{'invisible':1}"/>
<field name="field1" attrs="{'readonly':[('your_computed_field_that_check_login_user_group','=', False)]}"/>

Get complete code and detail about how to make field readonly: http://learnopenerp.blogspot.com/2016/10/how-to-visible-and-invisible-fields-in.html

Regards,

Sehrish

Avatar
Discard
Author

Hello Sehrish,

Thanks a lot for your help but this is not a solution for what i want.

In fact, I want to based on a checked group into Settings/users and through that a field will be alterable or not(readonly) by this user.

Author

Hello the solution is:

1/ add attrs="{'readonly':False}" to the field:

<field name="field1" widget="selection" attrs="{'readonly':False}"/>

2/Inherit the original view and do this modification:

<record id="xxx_form_specify" model="ir.ui.view">

<field name="name">xxx_form_specify</field>

<field name="model">xxx</field>

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

<field name="inherit_id" ref="module_contain_original_view.original_view_id"/>

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

<data>

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

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

</xpath>

</data>

</field>

</record>

3/Don't forget to test without admin account ==> because he can see all , he havn't limit access

@maram solution works.