Search code examples
xmlodooodoo-13odoo-14odoo-15

Odoo Add Multiple Group With Different Condition For the Same Field


Hope all is well for you guys, gday.

So I inherited a field called date_done from stock.picking model, which has readonly:true attribute by default. I revoked it to readonly but not in 'assigned' status for all Odoo user in the code below :

<field name="date_done" groups="base.group_user" position="attributes" >
    <attribute name="attrs">{'readonly':[('state','not in',('assigned'))],'invisible':['|',('state','not in',('assigned','done'))]}</attribute>
</field>

Then, i want to add one group called change_date_group_privilege that later will have readonly but not in 'assigned' + 'done' status attributes.

In the end, i expect that the base.group_user will only able to change field when the status is not in assigned, and change_date_group_privilege will be able to change when the status is not in assigned + done.

I cannot simply add change_date_group_privilege into groups="base.group_user", because i also need to add 'done' status inside readonly':[('state','not in',('assigned'))], which later will also give base.group_user privilege that i don't want.

How is it possible to add condition in this XML block of code? And if there's other way i'd be glad to get through it.

Thank you for your help.


Solution

  • Presumptions:

    • you have two level of access groups
    • one normal group (typically base.group_user "Internal Users")
    • and one special group "Change Date on Pickings when done" with id my_module.group_change_date_when_done

    So the normal Picking view looks like this (short example!)

    <record id="default_view" model="ir.ui.view">
        <field name="name">default example view</field>
        <field name="model">stock.picking</field>
        <field name="arch" type="xml">
            <form>
                <field name="date_done" readonly="1" />
                <field name="state" />
            </form>
        </field>
    </record>
    

    Now the requirements wants normal users to have the possibility to change the date_done field while the state is not assigned. So you extend and change the view for every normal user (no groups needed in view).

    <record id="default_view_extension_level1" model="ir.ui.view">
        <field name="name">default example view</field>
        <field name="model">stock.picking</field>
        <field name="inherit_id" ref="default_view" />
        <field name="arch" type="xml">
            <field name="date_done" position="attributes">
                <attribute name="readonly" />
                <attribute name="attrs">{'readonly':[('state','not in',('assigned'))]</attribute>
            </field>
        </field>
    </record>
    

    Now you can set on view on top of that and just add the special group to it. That will lead to a use of that special view only for users in that special group.

    <record id="default_view_extension_level2" model="ir.ui.view">
        <field name="name">default example view</field>
        <field name="model">stock.picking</field>
        <field name="inherit_id" ref="default_view_extension_level1" />
        <field name="groups_id" eval="[(4, ref('my_module.group_change_date_when_done'))]"/>
        <field name="arch" type="xml">
            <field name="date_done" position="replace">
                <field name="date_done" attrs="{'readonly':[('state','not in',('done', 'assigned'))]" />
            </field>
        </field>
    </record>
    

    In the end normal users will load the first two views and special users will also load the third view which will lead them to change the done date in done state.

    Keep in mind: that code is just example code to show the principle of extensions and using groups on views. It's not guaranteed to work!