How to apply attrs based on assigned groups to logged in user?

Hello everybody,

My Goal is to make field readonly based on assigned group to logged in user.

I want to make few field readonly based on assgined groups to logged in user.

I have tried these two methods to set attrs for perticular field.

For Eg, I want to make readonly field on SO form.

We have user_id field inside SO, so i have applied like this on xml part.

This doesn't  work!

<field name="field name" position="attributes">

<attribute name="attrs">{'readonly':[('group_name','in',[ for group in user_id.groups_id])]}</attribute>


This is too does not work!

<field name="field name" position="attributes">-->

<attribute name="attrs">{'readonly':[('group_name','in',[ for group in user.groups_id])]}</attribute>


Even we have user_id field inside the model, its give user_id field is not defiend. how?

I have tried to apply the same thing based on_change method too!

Is it possible to set attrs on field using onchange method?

Anyboday has idea about this?

Thanks in advance!



1 Comment

Yes you can apply attributes [visible/invisible, readonly] based on different users. The idea is to create a Boolean field and make this field True/False on onchange of some field, and later use that boolean field in attr.

Have a look into code:

Hope its helpful for you and others.


2 Answers

You could inherit your FORM view with a particular group access to modify readonly attribute.

Please check the sample code available here 


To add your group:
<field name="groups_id" eval="[(4, ref('MODULE_NAME.GROUP_EXTERNAL_ID'))]"/>

To replace other groups and add your group only: <field name="groups_id" eval="[(6,0,[ref('MODULE_NAME.GROUP_EXTERNAL_ID')])]" />

1 Comment

Thanks for this ans. but this will only set perticuler group to field right? or it will make it readonly that field rest of groups those are not assigned to that field?

Best Answer

Hi All,

I found solution to achive this goal.

Created boolean field on backend which set true based on parnter onchange method.

That onchange method will search that current logged in user is belongs to that specific group than it will return true for that boolean.

Based on boolean value i have applied attrs on field to make it readonly.