- 上传者: Administrator
- 上传时间:2023年11月19日 01时38分31秒
* 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 o ......
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.
本文章从网上收集,如有侵权请联系tderp@tderp.com删除
- 微信扫一扫,一分也是爱:
服务原则及地区范围
宜兴通达网络科技有限公司,地处中国宜兴环科园内,是一家高新技术企业。公司在企业网络维护和企业信息化建设与咨询方面,有10多年经验。
我公司愿与客户一道,力求彻底解决客户问题!
我们不是在给企业提供“头痛医头、脚痛医脚”的暂时解决方案,而是在部署根本性安全与稳定服务!!
我们愿携手客户,建立企业IT规划;杜绝随意安装系统、软件等操作;力求共同维护有序、安全、稳定的网络办公环境!!!
IT服务,服务是根本,客户是上帝;我们提供快速响应、快速上门、快速排查,提供优质高效的服务!!!!
通达科技提供全国范围内的服务,服务形式包括远程协助、电话咨询、电子邮件咨询、传真咨询、问答平台的问题解决等。
宜兴地区提供上门服务:
- 市区服务:宜城街道、城北街道(屺亭街道)、新街街道、新庄街道、环科园、渚桥开发区
- 市郊服务:张渚镇、西渚镇、太华镇、徐舍镇、官林镇、杨巷镇、新建镇、和桥镇、高塍镇、万石镇、周铁镇、芳桥镇、丁蜀镇、湖父镇。