Odoo Studio: field attributes based on User Access Groups

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:

14 thoughts on “Odoo Studio: field attributes based on User Access Groups

  1. 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 😉 )

    Like

    1. this is my code:
      for record in self:
      if self.env.user.has_group(‘base.group_erp_manager’):
      record[‘x_studio_auth’] = True

      Like

      1. Can you the post the full error text. There are various things that could be wrong!

        Like

  2. 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”

    Like

    1. I tested this with another “ordinary” user and I didn’t get any error. What version of Odoo are you using?

      Like

  3. when you check “stored” there is no error message but it doesn’t work. Awaiting function is not the good.

    Like

    1. 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).

      Like

      1. I already updated the post with the extra line of code. It’s the first line

        record[‘x_studio_authorization1’] = False

        Liked by 1 person

Leave a comment