author: @mayna @kawam tags:#odoo#python


Button Type

They are at least two types of button in Odoo, distinguished by their declaration inside the <button> tag.

  • Button Action. This type of button is defined in XML files, and it triggers an action when clicked. <button name="%(action_id)d" type="action" string="Button Label"/>

  • Button Object. This type of button requires a method definition in Python. <button name="button_method" type="object" string="Button Label"/>

Notes

  • When declaring actions inside XML files in Odoo, it’s essential to ensure that the action is declared before it is referenced in a form or tree view where a button is placed.

Example of Button Action

    <record id="ktc_kit_moves_action" model="ir.actions.act_window">
        <field name="name">Stock Moves</field>
        <field name="type">ir.actions.act_window</field>
        <field name="res_model">stock.move.line</field>
        <field name="view_mode">tree,form</field>
        <field name="domain">[('reference', '=', context.get('default_name'))]</field>
        <field name="help" type="html">
            <p>There's no product move yet.</p>
        </field>
    </record>
 
    <button class="oe_stat_button"
            name="%(ktc_kit_moves_action)d"
            string="Product Moves"
            type="action"
            icon="fa-exchange"
            states="done"
            context="{'default_name': name}"
    />
 

Example of Button Object

    def button_get_summary_ws_ids(self):
        for record in self:
            record.summary_ws_ids = [(5, 0, 0)]
            record.allocation_ws_ids = [(5, 0, 0)]
            month = record.period
            year = record.pyear
            if not month or not year:
                raise ValidationError(_("Please provide the month and year."))
            record.button_compute_running_ws()
            record.compute_summary_ws_ids(month, year)
            record.compute_allocation_ws_ids(month, year)
            record.prepare_allocation_data('running_ws')
    <button name="button_get_summary_ws_ids"
            type="object"
            string="Compute Workshop"
            class="oe_highlight"
            attrs="{'invisible':[('state','!=','running_ws')]}"/>

Example of Combination

    def button_goto_allocation_ws_ids(self):
        action = self.env.ref('hit_accounting_sn.gbs_action_allocation_workshop').read()[0]
        action['domain'] = [('id', 'in', self.allocation_ws_ids.ids)]
        return action
    <record id="gbs_action_allocation_workshop" model="ir.actions.act_window">
        <field name="name">Allocation Workshop Action</field>
        <field name="type">ir.actions.act_window</field>
        <field name="res_model">closing.sequence.allocation.workshop</field>
        <field name="view_mode">tree</field>
        <field name="help" type="html">
            <p class="oe_view_nocontent_create">
                No data.
            </p>
        </field>
    </record>
 
 
    <button name="button_goto_allocation_ws_ids"
            string="Allocation Workshop"
            type="object"/>