Sometimes, you will find that you have obtained recordsets which are not exactly what you need. This recipe shows various ways of combining them.
Here is the way to achieve common operations on recordsets:
result = recordset1 + recordset2
result = recordset1 | recordset2
result = recordset1 & recordset2
The class for recordsets implements various Python operator redefinitions, which are used here. Here is a summary table of the most useful Python operators that can be used on recordsets:
Operator |
Action performed |
---|---|
|
This returns a new recordset containing the records from |
|
This returns a new recordset consisting of the records from |
|
This returns a new recordset with all the records that belong to both |
|
This returns a new recordset with the records belonging to either |
|
|
|
|
|
|
|
|
There are also in-place operators +=
, -=
, &=
, and |=
, which modify the left-hand operand instead of creating a new recordset. These are very useful when updating a record's One2many
or Many2many
fields. See the Updating values of recordset records recipe previously for an example.
The sorted()
method will sort the records in a recordset. Called without arguments, the _order
attribute of the model will be used. Otherwise, a function can be passed to compute a comparison key in the same fashion as the Python built-in sorted(sequence, key)
function. The reverse
keyword argument is also supported.
Note about performance
When the default _order
parameter of the model is used, the sorting is delegated to the database and a new SELECT
function is performed to get the order. Otherwise, the sorting is performed by Odoo. Depending on what is being manipulated, and on the size of the recordsets, there can be some important performance differences.