Combining recordsets

Sometimes, you will find that you have obtained recordsets which are not exactly what you need. This recipe shows various ways of combining them.

Getting ready

To use this recipe, you need to have two or more recordsets for the same model.

How to do it…

Here is the way to achieve common operations on recordsets:

  1. To merge two recordsets into one while preserving their order, use the following operation:
    result = recordset1 + recordset2
  2. To merge two recordsets into one ensuring that there are no duplicates in the result, use the following operation:
    result = recordset1 | recordset2
  3. To find the records that are common to two recordsets, use the following operation:
    result = recordset1 & recordset2

How it works…

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

R1 + R2

This returns a new recordset containing the records from R1 followed by the records from R2. This can generate duplicate records in the recordset.

R1 – R2

This returns a new recordset consisting of the records from R1, which are not in R2. The order is preserved.

R1 & R2

This returns a new recordset with all the records that belong to both R1 and R2 (intersection of recordsets). The order is not preserved here.

R1 | R2

This returns a new recordset with the records belonging to either R1 or R2 (union of recordsets). The order is not preserved, but there are no duplicates.

R1 == R2

True if both recordsets contain the same records.

R1 <= R2R1 in R2

True if all records in R1 are also in R2. Both syntaxes are equivalent.

R1 >= R2R2 in R1

True if all records in R2 are also in R1. Both syntaxes are equivalent.

R1 != R2

True if R1 and R2 do not contain the same records.

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.

There's more…

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

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.245.196