Skip to Content
Menu
This question has been flagged
4 Replies
3604 Views

I am trying to make a field readonly for every user that does not belong to a specified group but it is not working I hope some one can help me, here is my code:

<page string="Item">
<field name="item_line_ids">
<tree editable="bottom">
<field name="document_name" column_invisible="True"/>
<field name="name" readonly="True"/>
<field name="number_of_pages" readonly="True"/>
<field name="document" widget="binary" filename="document_name"/>
<field name="state"/>
<field name="comment"
readonly="'groups_id' not in '[model_name.group_name]'"/>
</tree>
</field>
</page>

The fields in question are name, number of pages, state and comment.
In the code is an example of what I have tried but It does not worked for me

Avatar
Discard
Best Answer

In the Python model, create a computed field that checks if the current user is in the specified group.


from odoo import models, fields


class YourModel(models.Model):

    _name = 'your.model'


    # Other fields

    item_line_ids = fields.One2many('item.line', 'your_model_id')

    is_not_in_group = fields.Boolean(compute='_compute_is_not_in_group', store=False)


    def _compute_is_not_in_group(self):

        for record in self:

            # Replace 'your_module.group_name' with the actual external ID of your group

            record.is_not_in_group = not self.env.user.has_group('your_module.group_name')

Avatar
Discard
Author Best Answer

Thanks it works for me, I was trying to avoid computed fields but after some research I think is the solution.

Avatar
Discard
Best Answer

You can create a new computed field which will be a boolean type.  This field will compute whether the user belongs to that group or not. Use this field in the read-only attribute of any field.

Avatar
Discard
Best Answer

Hi,

To make a field read-only for users who do not belong to a specified group:

is_boolean_field = fields.Boolean(compute="_compute_is_boolean_field")

def _compute_is_boolean_field(self):

        for record in self:

            if not record.env.user.has_group("Provide the group that you want the restriction"):

                 record.is_boolean_field = True

            else:

                record.is_boolean_field = False

xml:

<page string="Item">

    <field name="item_line_ids">

        <tree editable="bottom">

            <field name="document_name" column_invisible="True"/>

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

            <field name="number_of_pages" readonly="True"/>

            <field name="document" widget="binary" filename="document_name"/>

            <field name="state"/>

            <field name="comment"

                   readonly="is_boolean_field == True"/>

        </tree>

    </field>

</page>


Hope it helps

Avatar
Discard
Related Posts Replies Views Activity
3
Dec 23
89760
3
Sep 21
4453
1
Sep 15
8522
3
Jul 23
5125
2
Mar 22
9939