How to define Related field in Odoo 17 - Odoo 17 Slides

CelineGeorge1 378 views 6 slides Jul 18, 2024
Slide 1
Slide 1 of 6
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6

About This Presentation

The related attribute is used in field definitions to establish a relationship between models and automatically fetch the value from a related model's field. It provides a way to reference and display fields from related models without having to create a separate field and write code to synchron...


Slide Content

How to define Related field in Odoo 17 Enterprise

Introduction Enterprise In Odoo, the fields can be made as related using the ‘related’ keyword. The related attribute is used in field definitions to establish a relationship between models and automatically fetch the value from a related model's field. It provides a way to reference and display fields from related models without having to create a separate field and write code to synchronize the values manually.

Enterprise The basic syntax for defining a related field is related_field = fields.FieldType(related='related.field.path', string='Field Label') where FieldType: The type of the field you are referencing related.field.path: The dot-separated path to the related field. Ie, it will be of the syntax related_model_id.field_name. This path’s first part related_model_id will be usually a Many2one field name in the same model. And field_name is the field name defined in that comodel. string: The label for the field in the user interface.

Enterprise To illustrate the usage of related fields, let’s create a simple model to store the details of an Student Exam Registration. The basic details are added in the model including a Many2one relational field student_id. class ExamRegister(models.Model): _name = ‘exam.register' _description = Exam Register’ student_id = fields.Many2one(string="Student Name", required=True) student_mobile = fields.Char(string="Mobile",related=’student_id.mobile’)

Enterprise Usually, related fields are set to read only because they are intended to reflect data from another model and not be edited directly. The related fields are not stored in the database. To improve read performance, it can be forced to save in the database by using the attribute store = True while defining the field.

For More Info. Check our company website for related blogs and Odoo book. Check our YouTube channel for functional and technical videos in Odoo. Enterprise www.cybrosys.com