How to Create Smart Buttons in Odoo 18

Hello Odooers & Developers!

Welcome to this Odoo technical blog, where I'll guide you step-by-step on creating Smart Buttons in Odoo. Bright buttons are the small clickable buttons at the top of form views in Odoo. With a count, they provide quick access to related data like tasks, invoices, sales, etc.

These buttons are not just visually helpful. They improve navigation and usability for end users.

What You Will Learn in This Guide:

  • What are Smart Buttons in Odoo?
  • How to define Smart Buttons using XML
  • How to link them to actions and models
  • How to display the count on each Smart Button
  • Real-world example using a custom model

What is a Smart Button in Odoo?

A Smart Button is a quick-access button displayed in the header of a form view. It usually:

  • Shows a count of related records (like the number of sales, tasks, or invoices)
  • Opens a related view (like list, form, or kanban)
  • Improves the user experience by minimizing clicks

You've probably seen them in modules like Sales, Invoicing, CRM, and Projects.

Example from Sales Order:

"Deliveries: 1" → Click it → See related delivery orders.

Let's Create Smart Buttons in a Custom Module

We'll walk through an example using a custom model called decoration.order.

Step 1: Define the Custom Model

Let's first define the model, which will include bright buttons.

File:models/decoration_order.py

.py

from odoo import models, fields, api

class DecorationOrder(models.Model):

    _name = 'decoration.order'
    _description = 'Decoration Order'
     task_count=fields.Integer(string='Task Count',compute='_compute_task_count')
	project_task_ids=fields.One2many('project.task','decoration_order_id',string='ProjectTask')
   def _compute_task_count(self):
    	    	for rec in self:

         counter = len(rec.project_task_ids.ids)

        	rec.task_count = counter

   Explanation:

  • project_task_ids: Links to project tasks.
  • task_count: A computed field that counts how many tasks are linked.

Step 2: Define the Related Model

.py

File:models/decoration_task.py

from odoo import models, fields

class DecorationTask(models.Model):

     _inherit='project.task'

decoration_order_id=fields.Many2one('decoration.order',string='decoration order')

This model represents tasks linked to each decoration order.

Step 3: Add the Smart Button in the Form View

Now that the models are ready, let’s define a smart button in the form view XML.

File:views/decoration_order_views.xml

XML

<odoo>

  <record id="view_decoration_order_form" model="ir.ui.view">
    <field name="name">decoration.order.form</field>
    <field name="model">decoration.order</field>
    <field name="arch" type="xml">
      <form string="Decoration Order">
        <sheet>
          <!-- Smart Buttons Section -->
          <div class="oe_button_box" name="button_box">
           <button type="object"
                    	name="view_task"
                    	class="oe_stat_button"
                    	invisible="task_count == 0"
                    	icon="fa-external-link">
                    	<field string="Task" name="task_count" widget="statinfo"/>
                	</button>
          </div>
        </sheet>
      </form>
    </field>
  </record>

</odoo>

Here Explanation:

  • oe_button_box: Container for smart buttons.
  • oe_stat_button: Makes it look like a smart button.
  • task_count: Shows the count.
  • action_view_tasks: The method that will open the related records.
  • view_task: Calls a Python method to show tasks.

Step 4: Define the Button Action in Python

Now define the method that will open related tasks when the smart button is clicked.

In models/decoration_order.py, add:

.py

def view_task(self):

    	project_task_ids = self.project_task_ids.ids
    	action = self.env.ref('project.action_view_all_task').sudo().read()[0]
    	action['context']=False
    	action['domain']=[('id','in',project_task_ids)]
    	if len(project_task_ids) > 1:
        	action['domain'] = [('id', 'in', project_task_ids)]
    	elif len(project_task_ids) == 1:
        	action['views'] = [(self.env.ref('project.view_task_form2').id, 'form')]
        	action['res_id'] = project_task_ids[0]
    	else:
        	action = {'type': 'ir.actions.act_window_close'}
    	return action

This method:

  • Opens a list and form view of project.task
  • Filters to only tasks related to this order
  • Automatically links new records to the current order

Testing the Smart Button

Now restart your Odoo server and upgrade your module.

Go to: Decoration > Orders > Open any record

You should now see a "Tasks" smart button with a number. Clicking it should show the related tasks in a list view.

If the record has no tasks, the count will be 0. Try creating a few tasks and watch the count update automatically.

Conclusion

Odoo Bright Buttons are robust, intuitive features that boost your form views. They allow users to go directly to relevant data in just one click, reducing time consumption and increasing. Devintellecs harnesses these groundbreaking features to minimize processes and productivity.


Odoo DEV May 6, 2025
Share this post
Archive
Sign in to leave a comment
Choosing the Right Odoo Training Center in Ahmedabad