search()
To implement business logic effectively in Odoo, it's often necessary
to retrieve records that meet specific conditions. This is
accomplished using the search() method.
For example, the following code searches for partners whose company
type is set to 'company'
domain = [('company_type','=','company')]
partner = self.env['res.partner'].search(domain)
>>>res.partner(1,2,3,6,9,10,13,15,16,17)
Upon invoking the search method, it provides the domain—a set of
criteria. Each criterion comprises a triple (presented as either a
list or a tuple) containing (field_name, operator, value).
- field_name (str)
The field name of the current model
or traversing a relationship through a Many2one using
dot-notation, for instance, 'street' or 'partner_id.country.'
- operator (str)
A comparison operator is applied to compare the field_name with
the value. Permissible operators include:
- =: Equal to
- !=: Not equal to
- >: Greater than
- >=: Greater than or equal to
-
<: Less than
-
<=: Less than or equal to
- =?: Equals to or unset (returns true if the value is
either None or False; otherwise, it acts like =)
- =like: Field_name is matched to the value pattern. In
the pattern, an underscore _ represents (matches) any
single character, and the percent sign % matches any
string of 0 or more characters.
- like: Matches the field_name against %value% pattern.
Similar to =like, but before matching, wrap the value in
%.
- not like: Doesn’t match against the %value% pattern.
- ilike: Case-insensitive like.
- not ilike: Case-insensitive not like.
- =ilike: Case-insensitive =like.
- not in: Is unequal to all of the items from value.
- child_of: Is a child (descendant) of a value record
(value can be either one item or a list of items). Takes
the semantics of the model into account, following the
relationship field named by _parent_name.
- parent_of: Is a parent (ascendant) of a value record
(value can be either one item or a list of items). Takes
the semantics of the model into account, following the
relationship field named by _parent_name.
- Value
When using an operator, the variable type must
be comparable to the named field.
Criteria in a domain can be combined using the following prefix
logical operators:
- '&' for logical AND
- '|' for logical OR
- '!' for logical NOT
Note:
Prefix logical operators in Odoo, such as !, are primarily used to
negate combinations of search criteria. While most individual
conditions have a straightforward negative counterpart (e.g., =
becomes !=, < becomes>=), prefix negation is particularly useful
when inverting compound domains that involve multiple
conditions..
Example:
To search for partners named ABC, from Belgium or Germany, whose
language is not English, you can use the following domain criteria:
[('name', '=', 'ABC'),
('language.code', '!=', 'en_US'),
'|', ('country_id.code', '=', 'be'),
('country_id.code', '=', 'de')]
In the search method, other matching keyword arguments are
considered.
- offset=N: Skip the initial N records matching the query. The
default is 0.
- limit=N: Return N records. There is no limit by default.
- order=sort_specification: Default sorting is based on the _order
attribute of the model class.
- count=Boolean: If True, return the number of records in the
record set; default is False.
Note:
Both the search() method with the count attribute and the
search_count() function yield identical results.