How to Add Statistic Buttons on Odoo Form?

and display a statistic value

vITraining Admin

Let's assume that we are working on a SCRUM environment and have a One2many field on the Project object to Release object.

class project(models.Model):
    _name = 'project.project'
_inherit = 'project.project'

    epic_ids = fields.One2many(comodel_name="vit_scrum.epic", inverse_name="project_id", string="Epics", required=False, )
release_ids = fields.One2many(comodel_name="vit_scrum.release", inverse_name="project_id", string="Releases", required=False, )

Now we need to display the number of Releases on a particular Project. We need to add a new button on the Project form, showing the total number of Releases for that Project. For example:




 

When the Release button is clicked, then the Releases corresponding to that Project is displayed.











Also, when we click Create button on the Release list, then the project field is automatically filled with the current Project.






















Here's how to do that:

First we need a functional field to calculate the total number of Releases the Project has:

class project(models.Model):
    _name = 'project.project'
 _inherit = 'project.project'
    epic_ids = fields.One2many(comodel_name="vit_scrum.epic", inverse_name="project_id", string="Epics", required=False, )
 release_ids = fields.One2many(comodel_name="vit_scrum.release", inverse_name="project_id", string="Releases", required=False, )

    def _compute_release_count(self):
        for project in self:
            project.release_count = len(project.release_ids)

release_count = fields.Integer(string="Release Count", required=False, compute='_compute_release_count')


Then, we modify the Project form, by adding our new button and displaying the number of Release inside:

<record id="project_scrum" model="ir.ui.view">
 <field name="name">project_scrum</field>
 <field name="model">project.project</field>
 <field name="inherit_id" ref="project.edit_project"/>
 <field name="arch" type="xml">
        <div name="button_box" position="inside">
  <button class="oe_stat_button"
type="action"
 name="%(act_open_vit_scrum_release_view)d"
context="{'search_default_project_id': active_id, 'default_project_id': active_id}"
icon="fa-tasks">
 <field string="Releases" name="release_count" widget="statinfo" />
 </button>
 </div>
</record>


What we did here:

  1. Inherit the project form by inserting the new statistic button inside the <div name="button_box">

  2. The button type of "action"

  3. The button name is the name of the  action window we need to open, in this case the decimal number of the action window that has the ID of act_open_vit_scrum_release_view

  4. The most important part is the context which is the default context to be used as the default filter at the time the action is opened, which is search_default_project_id = active_id and the default_project_id = active_id to set the default value of the form containing the project_id field

  5. The active_id is the currently opened Project Id.




This topic is part of the "Advanced Odoo Programming Techniques" provided by vitraining.com. For more information please visit shop.vitraining.com