Add a Smart Button using Odoo Studio

This is a good example of how Odoo Studio can quickly add useful functionality, and it’s also interesting to see what happens “behind the scenes” so that this can be replicated without Studio.

Enable Odoo Studio and move your mouse to the row of Smart Buttons:

Click on the ‘+’ button shown above. A window will be displayed for you to select the label, field and icon.

Select an icon and click on ‘Confirm’

This is the Smart Button

It shows POS Orders for a customer. The number tells you how many orders, and you can click on it to see the details:

Let’s take a look at the button:

In Developer Mode we can see the field that has been added by Studio and also the name of the widget (statinfo).

Field

Here’s the field

For Computed Fields the value will be calculated when needed, so in this example the field is NOT stored. If it is stored, dependencies need to be defined, otherwise the value will never be re-calculated.

The important part here is the “Compute” section:

results = self.env['pos.order'].read_group([('partner_id', 'in', self.ids)], 'partner_id', 'partner_id')
dic = {}
for x in results: dic[x['partner_id'][0]] = x['partner_id_count']
for record in self: record['x_partner_id__pos_order_count'] = dic.get(record.id, 0)

This Python code is counting the number of records on pos.order for this partner (customer).

Odoo Studio also generated an Extension View for the button. We can find it by navigating to Settings / Technical / (User Interface) / Views and search for “studio” and model = res.partner

This is the XML for the button:

<button class="oe_stat_button" icon="fa-shopping-basket" type="action" name="371">
<field name="x_partner_id__pos_order_count" string="POS" widget="statinfo"/>
</button>

This tells us that

  • class="oe_stat_button"
  • the icon is the shopping basket (you can choose other icons)
  • there is a Windows Action (ID = 371) – see below
  • the field name
  • the description is “POS”
  • widget statinfo is used

Windows Action

You will see from the XML that there is a Windows Action (ID = 371). This controls what happens when we click on the “smart button”:

Domain        [('partner_id', '=', active_id)]

Context:    {'search_default_partner_id': active_id,
    'default_partner_id': active_id}

7 thoughts on “Add a Smart Button using Odoo Studio

  1. Hi, I’m using Odoo 16 SaaS. In the products form, I removed a smart button through Studio by clicking the “remove from view” red button. Now I want to bring it back. How can I do it?

    Like

    1. Simple way is to go into Studio, click on the View tab on the top left and click on the Red Button – restore default view.

      Or enable Developer Mode and select the Debug menu, select Edit Form View, and then you should see an inherited Studio view and you can remove the change.

      Like

Leave a comment