The class for recordsets implements various Python operator redefinitions, which are used here. Here's 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 that 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 <= R2 R1 in R2 |
True if all records in R1 are also in R2. Both syntaxes are equivalent. |
R1 >= R2 R2 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 side operand instead of creating a new recordset. These are very useful when updating a record's One2many or Many2many fields. Refer to the Updating values of recordset records recipe for an example of this.