Odoo 17 Development Book: Traversing through Record

Traversing through Recordset:

A record set is defined as a collection of related items or records. A single record or record set of length 1 simplifies access to any field value. When there are multiple records, it is simply not possible to access any field value of the record by simply referring to the field name. If the length of the record set is greater than one, an error will be generated when using the dot method to access any field value. ( Eg: self.partner_id.name).

Consider the model res.partner, which contains all of our Odoo Database's contact records. If the partner variable contains a res.partner record, we can simply use partner.name to access a record that contains the name of a contact. It is not possible if the partner record set contains more than one record.

The mapped() method can traverse a record set with multiple lengths.

Using an example, let's discuss the process.

Consider the two models, student.parent and student.student, which store the records of the students and parents of an educational institution.

from odoo import fields, models


class StudentStudent(models.Model):
    _name = "student.student"
    _description = "Student"

    name = fields.Char(string="Name", required=True)
    phone = fields.Char(string="Phone Number")
    email = fields.Char(string="Email")
    partner_id = fields.Many2one('res.partner', string="Partner", required=True)
    date = fields.Date(string="Date", default=fields.Date.today())
    parent_id = fields.Many2one('student.parent', string="Parent")
from odoo import fields, models


class StudentParent(models.Model):
    _name = "student.parent"
    _description = "Parent"

    name = fields.Char(string="Name", required=True)
    phone = fields.Char(string="Phone Number")
    email = fields.Char(string="Email")
    partner_id = fields.Many2one('res.partner', string="Partner", required=True)
    date = fields.Date(string="Date", default=fields.Date.today())
    student_ids = fields.Many2many('student.student', string="Students")

The Parent model includes a relational field called student_ids that stores all students who are related to each other. If the student_ids field contains a single student record, refer to student_ids.name to get the name of the corresponding student. It is, however, impossible if the student_ids field contains multiple student records. We can define a method called get_student_name() to retrieve all of the names of students. The mapped() method can be used to retrieve all student names from a recordset of student.student.

def _get_students_name(self, parents):
    names = parents.mapped('student_ids.name')

In this case, the mapped(path) method will iterate through the recordset's fields, where the path is a string containing the field name separated by dots. The mapped() method will generate a new recordset that contains all of the fields in the path to all elements in the current recordset that are related to that field. For each field in the path, this recordset is created. In this example, a new recordset will be created for the fields name and student_ids.

The names variable will contain a list of all student names. This can also be used to return a recordset. If a relational field is provided as the path, the mapped() method will return a recordset instead of any basic type field. In the case of a basic field, mapped() returns a Python list.

Keep in mind that using mapped() may not be efficient because it operates in memory within the Odoo server by repeatedly traversing relations and thus performing SQL queries. In such cases, it is preferable to use the search() method with an appropriate domain.

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