Odoo supports a variety of views, including a tree, graph, kanban, pivot, and many others. Furthermore, Odoo reports can be obtained in a variety of formats, including pivot reports, graph reports, and others.
This blog will discuss how to inherit Odoo's existing pivot view.
Inheriting Views
To demonstrate the view of inheritance, let's look at a sales report. The below screenshot gives a view of the sales analysis report.
In the screenshot above, we have the sales teams at the top (column) and the date on the left side (row). Here I will reverse the field i.e., make Date as a column and Sales Team a row. Now to inherit the view, we need to inherit the sale.report model, which can be done using the code shown below.
class PivotInheritReport(models.Model):
_inherit = 'sale.report'
The pivot view must then be inherited, which can be accomplished with the following command.
<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>
The external ID of the pivot report is sale.view_order_product_pivot. Here we inherit and replace the fields and specify the date field type as a column and the team_id field as a row.
The screenshot below shows the outcome of the Analysis report after inheritance.
Additionally, follow these steps if you want to add newly added fields or other fields to the measure dropdown list.
Set the field type to measure.
For example:
<field name="amount_total" type="measure"/>
This will add this field to the drop-down list of measures. The preceding example represents an existing field in the sales pivot view.
Additionally, we can add more fields as rows or columns. Let's take a look at an example to find out how to configure multiple fields in rows and columns.
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>
Here, I have added the company field as a column. This field appears as a subfield [expanded field] of the date field if the following code is added.
We can use the below command if we need to replace the default field in the pivot view.
<xpath expr="//field[@name='team_id']" position="replace">
<field name="company_id" type="col"/>
</xpath>
Adding a new Field to the Report
If you want to add a new field to a report, you must inherit the model and add the new fields to it. Normally, a report is created using SQL views. The field values are therefore retrieved from the query.
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)
Here I need to get the value of the field from the query, so I am getting it from the sales order [fields['new_name'] = s.new_name as new_name]. This is because we also added a new field to Sales Orders [Sale]. Order] model too. If I simply add the field to the sales.report model without calculating or retrieving the value of the field and then add it to the view, I get an error. So use a query to calculate the value of the newly added field. You can add this field to your pivot view. If you want it to appear in the measure dropdown type 'measures' in rows or columns in the pivot table, and enter as rows or columns: as I explained above.
In this way, we can inherit the existing pivot view in Odoo15.