Search code examples
odooauthorizationodoo-9

Odoo 9 Is there a way to handle authorization with different groups on a certain field in form view?


I'm trying to create a form view.

<field name="is_positive" attrs="{'readonly':[('state','==','final')]}"/>

However there is many attributes like groups and invisible related to authorization so that certain group of people can see the field.

groups="base.group_hr_user"

But Is there a way for certain group can edit the field and the other group cannot?


Solution

  • First of all, you cannot use a domain like this one

    <field name="is_positive" attrs="{'readonly':[('state','==','final')]}"/>
    

    There is not a '==' operator, use = instead.

    Now, to answer your question, if you want to create a special view for another group in which some elements are readonly for one group, and editable in the other, you have to it this way.

    For the default view :

    <record id="some_model_view" model="ir.ui.view">
        <field name="name">some.model.form</field>
        <field name="model">some.model</field>
        <field name="arch" type="xml">
            <form>
                 <field name="some_field" readonly="1"/>
            </form>
        <field/>
    </record>
    

    For a certain group :

    <record id="some_model_view_for_other_group" model="ir.ui.view">
        <field name="name">some.model.form</field>
        <field name="model">some.model</field>
        <field name="inherit_id" ref="my_module.some_model_view"
        <field name="groups_id" eval="[(6, 0, [ref('some.first_group')])]" />
        <field name="arch" type="xml">
            <field name="some_field" position="attributes">
                <attribute name="readonly">0</attribute>
            </field>
        <field/>
    </record>