A recordset is considered to be the set of records remaining to the same model. It can be either in the form of a single record or collection of records. One can utilize decorators like ‘@one’, ‘@multi’ etc for controlling the records in a recordset.
filtered() helps to optimize and simplify the code by avoiding unwanted loop conditions to reach the condition. filtered() returns a set of records(recordset) which met the given condition on an existing recordset. We can use the built-in python function lambda (anonymous function) with filtered() to filter out the required records.
partner_ids = self.env['res.partner'].search()
supplier_ids = partner_ids.filtered(lambda l: l.supplier)
customer_ids = partner_ids.filtered(lambda l: l.customer)
In the above example, the variable partner_ids stores all the records from the table ‘res.partner’ as a recordset. We need to get all the suppliers from this same recordset as a separate recordset to manipulate some other kind of operations. For that, we do not need to search the entire database table (res_partner) again. We will be able to find out the suppliers from this same record set by the help of filtered and lambda functions. The same thing can be done for the customers also by checking the field ‘customer’ is True.
sorted() helps to sort out a recordset based on a given column name passed as its argument (string). It is the same as a list sorting method in python. The first argument of the sorted() accepts an iterable loop like lambda, and the second argument accepts a comparison key to check throughout the iteration(like str(l.name).lower). The third argument accepts a boolean value to reverse looping values.
mapped() takes a very important role in code optimization. It avoids user-defined loops. It returns a set of field values. Its argument accepts a string value as the column name and returns all the possible values from the recordset.
order_ids = self.env['sale.order'].search()
order_names = order_ids.mapped('name')
In the above snippet of codes, order_ids stores all the values from the model sale.order. We have to get all the values from the field name to do some other stuff. Then we can use the function mapped() to achieve this. It returns a list with all possible names of the table sale.order. The same function returns an object reference if we are using any Many2one fields like partner_id. Overall, the mapped() will check the behavior (type) of the given field and will return the values as an object or list based on the argument(field) type.
Sets in python cannot be modified or changed. Since this type of data type is immutable. So, the recordset in Odoo is also immutable. To overcome this issue, Odoo returns a new recordset which combined using various set operations if all the values are from the same model. Those set operations are :
Rec1 < Rec2 or Rec1 <= Rec2:
It checks whether rec1 is a subset of rec2.
Rec1 > Rec2 or Rec1 >= Rec2:
It checks whether rec1 is a super set of rec2.
Rec1 | rec2:
It returns a new recordset that contains the union of the set1 and set2 recordsets. In other words, this operator returns a new recordset that contains all the values from both sets.
Rec1 & rec2:
It returns a new recordset that contains the intersection of the set1 and set2 recordsets. In other words, this operator returns a new recordset that contains all common values from both sets.
Rec1 - Rec2:
It returns a new recordset which contains the values of rec1 which are not in Rec1.