In Odoo it’s easy to make a field visible only for certain groups of users (and therefore invisible for all other users).
It’s not so easy to make a field Read-Only for selected users (or set other attributes). Fortunately there is a fairly simple way to do it with only a few lines of Python.
Step One – add a new Boolean field using Studio

Step Two – edit field information to make it a computed field
You can click on the “More” button shown above, or navigate to Settings / Technical / (Database Structure) / Fields:

Stored: False
Dependencies: x_studio_authorization1
Compute (Python Code):
for record in self:
record['x_studio_authorization1'] = False
if self.env.user.has_group('<UserAccessGroup>'):
record['x_studio_authorization1'] = True
You can select any User Access Group to control this. In the above screenshot we used product.group_stock_packaging
(Manage Product Packaging), but you can choose whatever you want.
Step three: use this boolean field to control attributes. In this example we will make the ‘Barcode’ field non-display if the user is not a member of the User Access Group.

Of course you can make the Boolean field non-display later when it’s working (but you can’t remove it).
This is the XML generated by Studio:
<xpath expr="//field[@name='barcode']" position="attributes">
<attribute name="readonly": [["x_studio_authorization1","=",False]]}</attribute>
</xpath>
In simplified form, it would be like this:
<field name="barcode" attrs="{'readonly': [["x_studio_authorization1","=",False]]}"/>
This doesn’t work for creating a new record
The boolean field is not set, so no-one can enter a barcode!
A possible workaround is to set a default for selected users:

Hi,
thanks for the tips but when I’m try it I have this error message when I want to see my form (lead/opportunity):
crm.lead(76,).x_studio_auth
(x_studio_auth is the name of my field 😉 )
LikeLike
this is my code:
for record in self:
if self.env.user.has_group(‘base.group_erp_manager’):
record[‘x_studio_auth’] = True
LikeLike
Can you the post the full error text. There are various things that could be wrong!
LikeLike
yeah, that’s all: crm.lead(76,).x_studio_auth in a message box called “Something went wrong !”
LikeLike
I precise there is no error when I’m connected under administrator profile (who is on the good group “group_erp_manager”), only if I’m a “standard” user, not in “group_erp_manager”
LikeLike
I tested this with another “ordinary” user and I didn’t get any error. What version of Odoo are you using?
LikeLike
Odoo 13.
LikeLike
Yes, it seems not to work in Odoo 13. I will try to find out why!
LikeLike
when you check “stored” there is no error message but it doesn’t work. Awaiting function is not the good.
LikeLike
Yes, you are correct. My working theory is that it’s because the computed field is calculated as “superuser” when it’s stored, but not when it isn’t (could be wrong).
LikeLike
Updated to add a line to Python code (fix provided by Kanak Info Systems).
LikeLike
any idea when the update will applied?
LikeLike
I already updated the post with the extra line of code. It’s the first line
record[‘x_studio_authorization1’] = False
LikeLiked by 1 person
thanks! Very great job !
LikeLike