2

In previous version (15.0 and below) we are able to inherit a views and set the attributes to certain fields based on group assigned using groups_id.

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

We can later change a certain attributes to a fields, for example using XPath this way:

<xpath expr="//field[@name='field_name']" position="attributes">
  <attribute name="readonly">1</attribute>
</xpath>

The field_name will later become readonly based group on which user is assigned into module_name.group_name. However, I don't quite understand how to do it in Odoo 16. The docs stated that we are able to define group to user this way

<field name="field_name" groups="module_name.group_name" readonly="1"/>

This works only for a custom field that we created. When i applied using XPath, especially when inheriting available views and doing attributes to a created field, it's either will makes it readonly to all users or not working at all. Here's my attempt to apply an attributes to employee_id in attendance module :

<xpath expr="//field[@name='employee_id']" position="attributes">
  <t groups="check_in_location.group_attendance_basic">
    <field name="employee_id" readonly="1"/>
  </t>
</xpath>

This, however doesn't change anything. the check_in_location.group_attendance_basic is a separate group that i created and it don't inherit all other default group from attendance.

How is it possible to apply attributes to field based on a group? in this case, a readonly?

Thanks

Edit : Using this is working, but other group cannot see that field

<xpath expr="//field[@name='employee_id']" position="attributes">
            <attribute name="groups">check_in_location.group_attendance_basic</attribute>
            <attribute name="readonly">1</attribute>
        </xpath>
altela
  • 306
  • 2
  • 13
  • In your example, the `groups` attribute is applied to `t` tag – Kenly Aug 14 '23 at 05:11
  • Hello @Kenly , i tried another way by changing the XPath (kindly please see the edit). The field successfully become readonly to the assigned group, but other group cannot see the field even the administrator , – altela Aug 14 '23 at 05:40
  • You mix things up. Do you want to hide fields or make them readonly? Mixing readonly and groups will lead to field only seen by that or the groups (more than one is possible) but as readonly. – CZoellner Aug 14 '23 at 08:43
  • [groups](https://www.odoo.com/documentation/15.0/fr/developer/reference/backend/views.html#semantic-components) attribute is used to display the field for specific users only – Kenly Aug 14 '23 at 09:10
  • i mean to make fields readonly for all user within check_in_location.group_attendance_basic group. it's working and makes employee_id becomes readonly for that group, however another user which is not within check_in_location.group_attendance_basic suddenly becomes unable to see this employee_id field (i dont mean to hide it). – altela Aug 14 '23 at 09:20
  • my goals is to have similar functionality like Odoo 15, which is using but in Odoo 16 this method is no longer can be used. – altela Aug 14 '23 at 09:22
  • You mean the `groups_id` field of a view? That's a completely other thing, but yes that was a possible solution, but in Odoo 16 there is check preventing it. You could add a computed boolean field, doing the check on the users groups. Just add it to the view invisible and add a attrs like `{'readonly': [('my_boolean_field', '=', True)]}` to your field. – CZoellner Aug 14 '23 at 10:05
  • 2
    You can add another `employee_id` field and set `groups` to `!check_in_location.group_attendance_basic` – Kenly Aug 14 '23 at 11:05
  • @Kenly it's possible to invert the groups by using a `!`? – CZoellner Aug 14 '23 at 11:13
  • Yes you can use `!` in groups to specify that the user is not a member of the group as mentioned in the [user_has_group](https://github.com/odoo/odoo/blob/16.0/odoo/models.py#L1430) function docstring (Used in views [_postprocess_access_rights](https://github.com/odoo/odoo/blob/16.0/odoo/addons/base/models/ir_ui_view.py#L1059) ). A similar code can be found in [sale_project](https://github.com/odoo/odoo/blob/16.0/addons/sale_project/views/project_task_views.xml#L81) module – Kenly Aug 15 '23 at 14:00

1 Answers1

1

As @kenly suggested, i successfully managed to find the solution using code below :

<odoo>
<data>
    <record model="ir.ui.view" id="hr_attendance_user">
        <field name="name">hr_attendance_user_view</field>
        <field name="model">hr.attendance</field>
        <field name="inherit_id" ref="hr_attendance.view_attendance_tree"/>
        <field name="arch" type="xml">

            <!-- Applying this single XPath will makes employee_id invisible to other group and visible for check_in_location.group_attendance_basic only -->
            <!-- Even though it become invisible, the readonly attribute is working to check_in_location.group_attendance_basic group as my intention -->
            <xpath expr="//field[@name='employee_id']" position="attributes">
                <attribute name="readonly">1</attribute>
                <attribute name="groups">check_in_location.group_attendance_basic</attribute>
            </xpath>


            <!-- Later on, if i add this, it will show employee_id to all group except check_in_location.group_attendance_basic -->
            <!-- Because it's using exclamation mark, which means "This rule should apply to group which is NOT check_in_location.group_attendance_basic" -->
            <xpath expr="//field[@name='employee_id']" position="after">
                <field name="employee_id" groups="!check_in_location.group_attendance_basic"/>
            </xpath>

        </field>
    </record>
</data>

I don't really understand why odoo changing the previous behavior, and i don't really know if this solution is a concrete one XD perhaps i will keep this open in case there's a "good practice" solution.

Thank you so much @Kenly and @CZoellner !

altela
  • 306
  • 2
  • 13
  • It was removed to [get rid of the use of `groups_id` for backend views](https://github.com/odoo/odoo/pull/98551/commits/3d14d066d2236569ec78ab4908e8e32bac51e2f7) to be able to cache the arch of the view more easily. You can find a detailed explanation at [base: remove groups_id from ir.ui.view](https://github.com/odoo/odoo/pull/98551) You can find an example in [sale_project](https://github.com/odoo/odoo/blob/16.0/addons/sale_project/views/project_task_views.xml#L113-L114) where Odoo duplicates a field and use `groups='!group'` to make it readonly for non sale mans – Kenly Aug 15 '23 at 23:08