How to Add Stat Buttons in Odoo

Abid Patel
25-Oct-2024 Updated : 25-Oct-2024

Learn how to add stat buttons in Odoo form views to display metrics or counts, offering users quick insights and access to related records.

How to Add Stat Buttons in Odoo

Adding stat buttons in Odoo is a great way to provide a quick summary or key metrics directly in the form view. Stat buttons are often used to display information like record counts, totals, or other calculated values. They appear as clickable buttons in the top of the form view, allowing users to easily access related data.

What Are Stat Buttons in Odoo?

In Odoo, stat buttons are designed to show numeric summaries, counts, or key statistics of related records. These buttons can be configured to show totals, counts of related records, or any other computed information. They are usually displayed at the top of the form view, allowing users to click on them to access the related data.

How to Add Stat Buttons in Odoo

To create stat buttons, you typically need to use the following elements:

  • XML – Define the stat button in the form view.
  • Python – Add computed fields or methods that the stat button will display.

Adding Stat Buttons to Your Form View

Here is how you can add a simple stat button to a form view in Odoo:

Step 1: Define the Computed Field in Python

First, define a computed field in your model. For example, let’s create a computed field to count the number of related records:

python
<from odoo import models, fields>
    class CustomModel(models.Model):
        _name = 'custom.model'
        
        order_count = fields.Integer(string="Order Count", compute="_compute_order_count")
        def _compute_order_count(self):
            for record in self:
                record.order_count = self.env['sale.order'].search_count([('partner_id', '=', record.id)])
    

In this code:

  • order_count: A computed field to hold the count of related sale orders.
  • _compute_order_count: Method to calculate and set the count for order_count.

Step 2: Add the Stat Button in XML

Next, you need to add the stat button to your form view XML:

xml
<record id="view_custom_model_form" model="ir.ui.view">
        <field name="name">custom.model.form</field>
        <field name="model">custom.model</field>
        <field name="arch" type="xml">
            <form string="Custom Model">
                <header>
                    <button name="action_view_orders" type="object" class="oe_stat_button">
                        <field name="order_count" widget="statinfo" icon="fa fa-shopping-cart"/>
                    </button>
                </header>
            </form>
        </field>
    </record>
    

Explanation of the XML structure:

  • button: The button with oe_stat_button class to style it as a stat button.
  • field name="order_count": The computed field to be displayed, with the statinfo widget applied.

Step 3: Define the Button Action in Python (Optional)

If you want the stat button to perform an action (e.g., open a related records view), you can add a method:

python
<from odoo import models, fields>
    class CustomModel(models.Model):
        _name = 'custom.model'
        
        def action_view_orders(self):
            return {
                'type': 'ir.actions.act_window',
                'name': 'Sales Orders',
                'view_mode': 'tree,form',
                'res_model': 'sale.order',
                'domain': [('partner_id', '=', self.id)],
            }
    

In this example, the action_view_orders method opens the related sale.order records associated with the current model record.

Conclusion

Using stat buttons is an effective way to display key metrics directly on the form view in Odoo, enhancing the user experience by offering quick and actionable insights. With a few simple steps, you can add dynamic buttons to display calculated data and provide direct access to related records.

Make a Comment

Your email address will not be published. Required fields are marked *