How it works...

Step 1 defines the method.

Step 2 creates a search domain in a local variable. Often, you'll see this creation inline in the call to search, but with complex domains, it is good practice to define it separately.

For a full explanation of the search domain syntax, refer to the Defining filters on record lists Domain recipe in Chapter 10, Backend Views.

Step 3 calls the search() method with the domain. The method returns a recordset that contains all the records that match the domain, which can then be further processed. In this recipe, we call the method with just the domain, but the following keyword arguments are also supported:

  • offset=N: This is used to skip the N first records that match the query. This can be used along with limit to implement pagination or to reduce memory consumption when processing a very large number of records. It defaults to 0.
  • limit=N: This indicates that, at most, N records should be returned. By default, there is no limit.
  • order=sort_specification: This is used to force the order on the returned recordset. By default, the order is given by the _order attribute of the model class.
  • count=boolean: If True, this returns the number of records instead of the recordset. It defaults to False.
We recommend using the search_count(domain) method rather than search(domain, count=True), as the name of the method conveys the behavior in a much clearer way; both will give the same result.

Sometimes, you need to search from another model so that searching on self will return a recordset of the current model. To search from another model, we need to get an empty recordset for the model. For example, let's say we want to search on the res.partner model because we want to search the contacts of the res.partner records. To do that, we would need to search on an empty recordset of the res.partner model. See the following code sample:

@api.multi
def find_partner(self):
PartnerObj = self.env['res.partner']
domain = [
'&', ('name', 'ilike', 'Parth Gajjar'),
('company_id.name', '=', 'Odoo')
]
partner = PartnerObj.search(domain)
..................Content has been hidden....................

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