Understanding Related Field In Odoo

Hello Odooers & Developers!

Welcome to this Odoo technical blog! Today, I’ll be sharing insights on how to use related fields in Odoo effectively. Related fields help you display information from one model in another without copying data. This makes data handling easier, speeds up your system, and keeps everything accurate and up to date. This blog will explore how to work with related fields and their practical use cases in Odoo.

  • What related fields are and how they work.
  • How to define a related field in Odoo.
  • Practical examples to demonstrate their usage.
  • Key benefits of using related fields.

What Are Related Fields in Odoo?

Related fields in Odoo allow you to show information from a linked model without storing extra data. They work with relationships like Many2one, One2many, and Many2many to fetch values automatically.

For example, in the sale.order model, there is a Many2one relation with res.partner (Customer). If you want to display the customer’s email in the sales order, you can use a related field to fetch it directly instead of storing it separately.

How to Define a Related Field in Odoo

you can define a related field using the related parameter in the field definition.

Example:

from odoo import models, fields

class SaleOrder(models.Model):

    _inherit = 'sale.order'

customer_email = fields.Char(related='partner_id.email',    string="Customer Email", store=True)

Explanation:

  • "partner_id" is a Many2one field linking to res.partner (Customer).

  • "email" is fetched dynamically from the res.partner model.

  • "store=True" ensures the field is saved in the database for filtering and searching.

Key benefits of using related fields

1. Easy Data Access

  • Related fields let you fetch data from other models without writing complicated code or queries.
  • Example: Instead of manually searching for a customer’s email in the customer record, you can directly display it in the sales order.

2. Better Performance

  • Odoo automatically retrieves data without requiring extra database searches, making the system faster.
  • Example: Instead of running a separate query to find a product’s category, a related field can fetch it instantly.

3. Accurate and Consistent Data

  • Related fields ensure that any changes in the original model are automatically reflected everywhere they are used.
  • Example: If an employee’s job title is updated, all attendance records will show the new title without needing manual updates.

Conclusion:

Related fields in Odoo make it easy to display and use data from related models without storing duplicate information. They help keep data accurate, improve system speed, and ensure everything stays up to date automatically.

By using options like store=True , you can make your Odoo modules more powerful and efficient.

FAQs of Related Fiels in Odoo

1. What is a related field in Odoo?

A related field in Odoo allows you to display information from another model without directly storing it in your current model. It links to a field in a related record and shows its value.

2. How does a related field work in Odoo?

A related field uses the related attribute in the model. It points to a field in another model, allowing you to retrieve and display that data as part of the current model.

3. Why should I use related fields?

Related fields help reduce redundancy by letting you display information from other models without duplicating data. It also makes your code cleaner and easier to manage.

4. Can I modify the value of a related field?

No, related fields are read-only. You cannot modify them directly because they are linked to another model's field. However, you can change the original field they reference.

5. How do I create a related field in Odoo?

To create a related field, you define it in your model and use the related attribute, pointing to the target field in the related model. For example, related='partner_id.name' will display the partner's name linked to a sale order.

6. Can I use related fields in views?

Yes, like regular fields, related fields can be added to form and tree views. They help display data from related models directly in the user interface.

I hope this guide helps you understand and use related fields in your Odoo projects easily. 

If you want odoo technical training on any odoo version , please let us know by mail at [email protected]. Then next, our odoo expoert will conduct online or offline training with you 


Devintellecs & team are odoo training providers in the USA and INDIA, so we will try our best to give the training either individal or any bulk employee company.


If you want to check your odoo technical or functional knowledge, then we have prepared the odoo EXam practice test for the odoo technical & functional people.



Odoo DEV February 11, 2025
Share this post
Archive
Sign in to leave a comment
Customizing Widget Percentpie in odoo