Traversing through Recordset:
A collection of related items or records can be referred to as a record set. A single record or a record set of length 1 makes it simpler to access any field value. If there are multiple records, accessing any field value of the record by simply referring to the field name is just not feasible. If the length of the record set is greater than one, an error will be generated when attempting to access any field value via the dot method. ( Eg: self.partner_ids.name).
Consider the model res.partner, which contains all of the Contact records saved in our Odoo Database. If we want to access a record that contains the name of a contact, then we can simply use partner.name if the partner variable holds a res.partner record. It is not feasible with more than one record in the partner record set.
A record set with more than one length can be traversed using the mapped() method.
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 Student(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 Parent(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 to store all students related named student_ids. If the student_ids field contains a single student record, the name of the corresponding student can be obtained by referring to student_ids.name. It is, however, impossible if there are multiple student records in the student_ids field. To retrieve all the names of students we can define a method called get_student_name(). To retrieve all student names from a recordset of student.student, we can use the mapped() method.
def _get_students_name(self, parents):
names = parents.mapped('student_ids.name')
Here, the mapped(path) method will traverse the fields of the recordset, where the path is a string containing the field name separated by dots. The mapped() method will create a new recordset that includes all the fields related to that field in the path to all elements in the current recordset. This recordset is created for each field given in the path. A new recordset will be created in this example for the fields name and student_ids.
names variable will contain a list of all names of students. A recordset can also be returned using this. Instead of returning any basic type field, the mapped() method will return a recordset if a relational field is provided as the path.mapped() will return a Python list in the case of a basic field.
One thing to keep in mind is that using mapped() may not be efficient because it operates in memory within the Odoo server by repeatedly traversing relations and thus making SQL queries. In such cases, using the search() method with an appropriate domain makes more sense.