Ibrahim Rahimi
Ibrahim Rahimi

Reputation: 539

How to get latest records of a model according to create date of record using search() method of ORM API in Odoo 9?

I want to access data of two latest records of a model using ORM API's record.search() method in Odoo 9. Is it possible to get the data of two latest records based on the value of create date field in a model? This is the code I am going to get the latest records with it:

@api.multi
def get_data(self, rec):
    reference_data = self.env['recuite.reference.reference'].search([('recruite_id', '=', rec.id)])

What parameter should I add to search method to get intended result?

Upvotes: 1

Views: 4477

Answers (1)

Kenly
Kenly

Reputation: 26718

You need to add two arguments to the search method:

, order='create_date desc', limit=2)  
Parameters
  • args -- A search domain. Use an empty list to match all records.
  • offset (int) -- number of results to ignore (default: none)
  • limit (int) -- maximum number of records to return (default: all)
  • order (str) -- sort string
  • count (bool) -- if True, only counts and returns the number of matching records (default: False)
Returns: at most limit records matching the search criteria
Raises: AccessError --
  • if user tries to bypass access rules for read on the requested object.

Upvotes: 5

Related Questions