Search code examples
odooodoo-8

How to make field readonly based on group and status?


I want to make field readonly based on group, and status. Like I have two groups:

  1. Manager Group
  2. User Group

If I give User Group to any user and then change Status to Done, then field will be readonly for this user.

Hope I was able to make it clear to understand. Thanks.


Solution

  • Create a functional field of type boolean. If the logged in user is under user group and state is done, then return true. Then in the view, specify attrs="{'readonly':[('boolean_field_name','=',True)]}"

    OR

    First create your form view. Then inherit the view also specify the groups. for example in sale order form view, i want to make the customer reference field readonly for group user when state is not in draft or sent.

    <record id="view_order_form_cust_ref_readonly" model="ir.ui.view">
        <field name="name">sale.order.form.readonly.cust</field>
        <field name="model">sale.order</field>
        <field name="inherit_id" ref="sale.view_order_form"/>
        <field name="groups_id" eval="[(6, 0, [ref('base.group_user') ])]"/>
        <field name="arch" type="xml">
            <field name='client_order_ref'" position="attributes">
                <attribute name="attrs">{'readonly':[('state','not in',['draft','sent'])]}</attribute>
            </field>
        </field>
    </record>