Enable Dark Mode!
how-can-we-inherit-existing-pivot-view-report-in-odoo-16.jpg
By: Hari Krishnan

How Can We Inherit Existing Pivot View Report in Odoo 16

Technical Odoo 16

Odoo offers a wide range of views, including tree, graph, kanban, pivot, and more. It also provides various formats for generating reports, such as pivot reports, graph reports, and others.

In this blog post, we will explore how to inherit Odoo's existing pivot view and customize it according to our requirements.

Inheriting View

To demonstrate view inheritance, let's consider a sales report. The following screenshot shows a Sales Analysis report.

How Can We Inherit Existing Pivot View Report in Odoo 16-cybrosys

In the above screenshot, the sales team is displayed in columns, and the dates are displayed in rows. Now, let's reverse these fields, i.e., display dates in columns and sales teams in rows. To achieve this, we need to inherit from the Sales module.

To inherit from the Sales module, we can use the following code:

class PivotInheritReport(models.Model):
   _inherit = 'sale.report'
Next, we need to inherit the perspective view. We can achieve this using the following code:
<record id="view_order_product_pivot_inherit" model="ir.ui.view">
<field name="name">sale.report.pivot.inherit</field>
    <field name="model">sale.report</field>
    <field name="inherit_id" ref="sale.view_order_product_pivot"/>
    <field name="arch" type="xml">
      <xpath expr="//field[@name='team_id']" position="replace">
            <field name="team_id" type="row"/>
    </xpath>
      <xpath expr="//field[@name='date']" position="replace">
            <field name="date" interval="month" type="col"/>
      </xpath>
    </field>
</record>
In the code snippet above, the external ID of the pivot report is "sale.view_order_product_pivot". We inherit and replace the fields and specify the date field as a column and the team_id field as a row.
The following screenshot illustrates the output of the sales analysis report after the inheritance.

How Can We Inherit Existing Pivot View Report in Odoo 16-cybrosys

Furthermore, if you want to add newly added fields or other fields to the measure dropdown list, you can follow these steps:

Set the field type to "measure". For example:
<field name="amount_total" type="measure"/>

This will add the field to the dropdown list of measures. In the above example, "amount_total" represents an existing field in the sales pivot view.

To add more fields as rows or columns, let's consider an example:

Example: Define the following code in an XML file.

<record id="view_order_product_pivot_inherit" model="ir.ui.view">
    <field name="name">sale.report.pivot.inherit</field>
    <field name="model">sale.report</field>
    <field name="inherit_id" ref="sale.view_order_product_pivot"/>
    <field name="arch" type="xml">
        <xpath expr="//field[@name='team_id']" position="after">
            <field name="company_id" type="col"/>
        </xpath>
    </field>
</record>

How Can We Inherit Existing Pivot View Report in Odoo 16-cybrosys

In the code above, we have added the "company" field as a column. This field will appear as a subfield (expanded field) of the date field when the above code is added.

To replace the default field in the pivot view, you can use the following command:

<xpath expr="//field[@name='team_id']" position="replace">
    <field name="company_id" type="col"/>
</xpath>

How Can We Inherit Existing Pivot View Report in Odoo 16-cybrosys

Adding a new Field to the Report

If you want to add a new field to a report, you need to inherit the model and add the new field to it. Generally, a report is created using SQL views, and the field values are retrieved from the query.

Here is an example of inheriting the "sale.report" model and adding a new field:

class PivotInheritReport(models.Model):
    _inherit = 'sale.report'
    new_name = fields.Char('New Name')
    def _query(self, with_clause='', fields={}, groupby='', from_clause=''):
        fields['new_name'] = ", s.new_name as new_name"
        return super(PivotInheritReport, self)._query(with_clause, fields, groupby, from_clause)
In the above code, we define a new field called "new_name" of type Char. Since we need to retrieve the field value from the query, we include it in the query definition. If we simply add the field to the "sale.report" model without retrieving its value, an error will occur. By using a query to calculate the value of the newly added field, we can include it in our pivot view and make it appear in the measure dropdown.
This is how we can inherit and customize the existing pivot view in Odoo 16.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp
location

Calicut

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

location

Kochi

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

location

Bangalore

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

Send Us A Message