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

Note that field is NOT stored. That’s because it is a Computed Field (the value will be computed when needed).

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}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s