Odoo 17 Development Book- Searching Records

Searching Records

To enable the functionality of business logic methods, it is necessary to locate records based on specific criteria. In Odoo, this task is achieved through the utilization of the search() method.

Consider this example: here, we are searching for partners whose 'company type' is labeled as '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:

These prefix logical operators are mainly used to negate combinations of criteria. Typically, individual criteria have a negative form (e.g., = -> !=, < -> >=), which is simpler than negating the positive.

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.

whatsapp
location

Calicut

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

location

Kochi

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

location

Bangalore

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

Send Us A Message