Search code examples
pythonpython-3.xodooodoo-13

How to set a sequence for Odoo smart buttons?


I am adding a new button inside the button_box div of products. To do that I wrote the following code:

<record id="product_template_form_view" model="ir.ui.view">
    <field name="name">product.template.common.form</field>
    <field name="model">product.template</field>
    <field name="inherit_id" ref="product.product_template_form_view"/>
    <field name="arch" type="xml">
        <div name="button_box" position="inside">
            <button class="oe_stat_button" type="object" name="open_maintenance_equipments" groups="maintenance.group_equipment_manager" icon="fa-wrench">
                <field string="Maintenance Equipments" name="maintenance_equipment_count" widget="statinfo"/>
            </button>
        </div>
    </field>
</record>

It works OK, but the button appears on the left side, before the Odoo main buttons of sale, purchase, etc. I would change the code to:

<record id="product_template_form_view" model="ir.ui.view">
    <field name="name">product.template.common.form</field>
    <field name="model">product.template</field>
    <field name="inherit_id" ref="product.product_template_form_view"/>
    <field name="arch" type="xml">
        <xpath expr="//div[@name='button_box']/button[last()]" position="after">
            <button class="oe_stat_button" type="object" name="open_maintenance_equipments" groups="maintenance.group_equipment_manager" icon="fa-wrench">
                <field string="Maintenance Equipments" name="maintenance_equipment_count" widget="statinfo"/>
            </button>
        </xpath>
    </field>
</record>

But the problem here would be that in the source view product.product_template_form_view there is still no button inside the div. So to apply this I should inherit from one of the main views which modify this product view (like the ones introduced by sale, purchase, etc apps). But I do not want to do so, because:

  1. My module should depend on apps which are not related to my module. I cannot automatically install for example purchase when installing my module.
  2. Despite doing this, it won't guarantee the fact that my button is after these "main" buttons, because it will depend on the module installation order.

So, do you know a way to achieve my purpose, like a sequence field or something like that?


Solution

  • Purchase and stock modules inherit the product.product_template_only_form_view view and sale module inherit the product.product_normal_form_view and you are inheriting the parent view which will always be fully resolved before the current view’s inheritance specs are applied:

    if the view has a parent, the parent is fully resolved then the current view’s inheritance specs are applied

    For more information, check the view resolution documentation

    If you had inherited one of the views above, the button would have automatically been added to the right, depending on the priority of the view, as already mentioned by @CZoellner