smart-buttons-odoo-13.png
Blogger_637068746542562782.png
By: Abdul Afras

How to Add Smart Buttons in Odoo 13

Technical Odoo 13

Sometimes we may need to add some related records of other models in our current form view. In Odoo we can do it easily with the help of smart buttons. Smart buttons allow seeing all the related records in the form view. We can also add a counter to count the number of records present in it. It is very useful to navigate to related documents or data very quickly. In this blog, I will be explaining how to add a smart button in the existing module.


smart-buttons-odoo-13-cybrosys


In the above image, we can see the smart buttons in the Contacts. From the smart button, we can see the opportunities of the Azure Interior or Meetings of Azure Interior, etc and also with the count.


Now I am going to add a new smart button in the contacts page to see the vehicle assigned to the corresponding partner in Fleet module.


Firstly, we need to inherit the form view. Followed add the smart button. The start button must be present inside the div with class oe_button_box. Here we don’t need to create a div with oe_button_box since it is already there we need to inherit and add a new button inside the class. Don't forget to add contacts and fleet as a dependency in the manifest.


<odoo>
    <data>
        <record id="fleet_vehicle_smart_button" model="ir.ui.view">
            <field name="name">fleet.view.buttons</field>
            <field name="model">res.partner</field>
            <field name="inherit_id" ref="base.view_partner_form" />
            <field name="arch" type="xml">
                <div name="button_box" position="inside">
                    <button class="oe_stat_button" type="object" name="get_vehicles"
                        icon="fa-car">
                        <field string="Vehicle" name="vehicle_count" widget="statinfo"/>
                    </button>
                </div>
            </field>
        </record>
    </data>
</odoo>


This code will add a smart button inside the Contacts.


smart-buttons-odoo-13-cybrosys


Here we can see the smart button. Now we will get an error when clicking on to the button since we don’t add the function ‘get_vehicles’ which we mentioned as the button name.


Now we will add the function get_vehicles.


from odoo import models, fields, api


class search(models.Model):
    _inherit = 'res.partner'
    vehicle_count = fields.Integer()
    def get_vehicles(self):
        self.ensure_one()
        return {
            'type': 'ir.actions.act_window',
            'name': 'Vehicles',
            'view_mode': 'tree',
            'res_model': 'fleet.vehicle',
            'domain': [('driver_id', '=', self.id)],
            'context': "{'create': False}"
        }

smart-buttons-odoo-13-cybrosys


Since we have given the domain as 'domain': [('driver_id', '=', self.id)], This allowed viewing the record of Brandon freeman in the fleet vehicle Module. 


Now the Smart button and the related data is displayed. We have one more thing to do. That is we need to compute the number of records present in and display it on the smart button. we have already added the field in the model as well as in the view. Now we need to add a function to compute a count.


For that, we need to add compute inside the field and give the function.


That is,    


vehicle_count = fields.Integer(compute='compute_count')


And the function compute_count is


    def compute_count(self):
        for record in self:
            record.vehicle_count = self.env['fleet.vehicle'].search_count(
                [('driver_id', '=', self.id)])

 

smart-buttons-odoo-13-cybrosys


Now we can see the count is been displayed.

 

In this way, you can add the smart button in any module or in the custom module.



cybrosys youtube

Comments

0


Leave a comment

 
Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

London

Cybrosys Limited
Alpha House,
100 Borough High Street, London,
SE1 1LB, United Kingdom

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message