In this article, we’ll demonstrate how to display computed fields in a tree view in Odoo using a real-life scenario. Let’s say you want to display the tags from sales orders in the respective invoices’ tree view. This can help your finance team quickly identify the context of invoices without navigating back to the sales order.To achieve this, we will define a computed field in the account.move model (Invoice model) that fetches related sales order tags and displays them in the invoice tree view.
Step 1: Define a Computed Field in the Invoice Model
First, we need to create a computed Many2many field in the account.move model that fetches tags from the related sales order. Here’s how you can do it:
python
from odoo import models, fields, api
class AccountMove(models.Model):
_inherit = ‘account.move’
invoice_tag_ids = fields.Many2many(
‘crm.tag’,
string=’Sales Tags’,
compute=’_compute_invoice_tags’
)
@api.depends(‘invoice_origin’)
def _compute_invoice_tags(self):
"""
Compute method to fetch sales order tags and assign them to the invoice.
"""
sale_order_model = self.env[‘sale.order’]
for invoice in self:
tags = []
if invoice.invoice_origin:
sale_order = sale_order_model.search([(‘name’, ‘=’, invoice.invoice_origin)], limit=1)
if sale_order:
tags = sale_order.tag_ids.ids
invoice.invoice_tag_ids = tags
Step 2: Use the Computed Field in the Tree View
Now that the computed field invoice_tag_ids is available, we can include it in the invoice tree view. We will use the widget=”many2many_tags” to display the tags in a visually appealing format. Additionally, we’ll use options=”{‘color_field’: ‘color’}” to add colors to the tags, enhancing readability.
Here’s how the XML view looks:
xml
<record id="view_account_move_tree" model="ir.ui.view">
<field name="name">account.move.tree</field>
<field name="model">account.move</field>
<field name="inherit_id" ref="account.view_move_tree"/>
<field name="arch" type="xml">
<xpath expr="//field[@name=’state’]" position="after">
<field name="invoice_tag_ids"
string="Tags"
widget="many2many_tags"
options="{‘color_field’: ‘color’}"/>
</xpath>
</field>
</record>
Real-Life Use Case
Imagine a company that uses Odoo to manage both sales and invoicing. The sales team adds relevant tags, such as “Priority Customer,” “Seasonal Discount,” or “New Product,” to their sales orders. When the finance team processes invoices, they need to know these tags to ensure proper handling.
By adding the computed field invoice_tag_ids to the invoice tree view, the finance team can immediately see the relevant tags for each invoice. This improves communication between departments and reduces errors.
Benefits of This Approach
- Improved Visibility: Sales-related tags are visible directly in the invoice view.
- Seamless Integration: The computed field pulls data dynamically without manual intervention.
- Customizable UI: Using the many2many_tags widget with color options enhances the user experience.
By implementing this feature in your Odoo ERP, you can bridge the gap between sales and finance, making your workflow more efficient and transparent. If you need further customization, Odoo allows you to adapt this approach to suit your business needs.