Chapter 4 - Odoo 15 Development Book

Traversing through Recordset

Traversing through Recordset:

A record set can be simply defined as a set of similar objects or records. It is easier to access any field value from a single record or a record set of length 1.When it comes to multiple records it is not possible to access any field value of the record simply by referring the field name. If that record set has length more than 1, then it will raise an error when trying to access any field value by dot method ( Eg: self.partner_ids.name).

As an example consider the model res.partner which holds all the records of Contacts saved in our Odoo Database. If we wan’t to access the name of a contact record, then simply we can use partner.nameif partner variable holds a res.partner record. It is not possible with more than one record in the partner record set.

mapped() method can be used to traverse through a record set of length more than one. Let’s discuss the method with the help of an example.

Consider 2 models student.parent and student.student which holds the records of Parents and Students 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")

Parent model contains a relational field to store all students related named student_ids. If the student_ids field have a single student record then it is possible to get the name of corresponding student by referring student_ids.name .. But if there are multiple student records in student_ids field, it is impossible. For getting all the name of students define a method called get_student_name(). Use mapped() method to get all names of students in a recordset of student.student.

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 will be a string that contains field name separated by dots. mapped() method will produce a new recordset that contains all 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.Here in this example, a new recordset will be created for the fields student_ids and name.

names variable will contain a list of all names of students.This can also be used to return a recordset. If a relational field is given to the path of mapped() method rather than any basic type field, it will return a recordset. In the case of basic field mapped() will be returning a python list.

One thing to keep in mind is that it may not be efficient to use mapped() as it operates in memory inside the Odoo server by repeatedly traversing relations and therefore making SQL queries.search() method with appropriate domain will make more sense in such cases.

whatsapp_icon
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