recordsets-and-operations-odoo.png
Blogger_636994549477427957.png
By: Mashood K

Recordsets and Its Operations in Odoo

Technical

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.


Operations


filtered() 

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. 


Example: 

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()

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()

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.


Other Operations

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.



cybrosys youtube

Comments

1

Odoo Haleem

Thank you very much. This is really helpful

16/12/2019

-

12:21AM




Leave a comment

 
Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

London

Cybrosys Limited
Alpha House,
100 Borough High Street, London,
SE1 1LB, United Kingdom

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message