pythonxmlodoo-16

Odoo 16 Make Fields Readonly Using XPath


i am currently using odoo 16, where i have created a custom field and trying to make that field readonly. The readonly field should appear only if a non-administrator user is logged in

This is my initial code:-

<odoo>
    <data>
        <record id="product_template_only_form_view" model="ir.ui.view">
            <field name="name">product.template.product.form</field>
            <field name="model">product.template</field>
            <field name="inherit_id" ref="product.product_template_only_form_view"/>
            <field name="arch" type="xml">
                <xpath expr="//group[@name='group_general']" position="inside">
                    <field name="recommanded_price" string="Recommended Price" readonly="1" groups="base.group_system"/>
                </xpath>
            </field>
        </record>
    </data>
</odoo>

What i tried?

<odoo>
    <data>
        <record id="product_template_only_form_view" model="ir.ui.view">
            <field name="name">product.template.product.form</field>
            <field name="model">product.template</field>
            <field name="inherit_id" ref="product.product_template_only_form_view"/>
            <field name="arch" type="xml">
                <xpath expr="//group[@name='group_general']" position="inside">
                    <field name="recommanded_price" string="Recommended Price" 
                           attrs="{'readonly': [('groups', 'not in', ['base.group_system'])]}"
                    />
                </xpath>
            </field>
        </record>
    </data>
</odoo>

Solution

  • Try this: for base.group_user, the field will become read-only, and for base.group_system, it will become editable.

    <record id="product_template_only_form_view" model="ir.ui.view">
        <field name="name">product.template.product.form</field>
        <field name="model">product.template</field>
        <field name="inherit_id" ref="product.product_template_only_form_view" />
        <field name="groups_id" eval="[(6, 0, [ref('base.group_system') ])]" />
        <field name="arch" type="xml">
            <field name="name" position="attributes">
                <attribute name="readonly">0</attribute>
            </field>
        </field>
    </record>
    
    <record id="product_template_only_form_view" model="ir.ui.view">
        <field name="name">product.template.product.form</field>
        <field name="model">product.template</field>
        <field name="inherit_id" ref="product.product_template_only_form_view" />
        <field name="groups_id" eval="[(6, 0, [ref('base.group_user') ])]" />
        <field name="arch" type="xml">
            <field name="name" position="attributes">
                <attribute name="readonly">1</attribute>
            </field>
        </field>
    </record>