In Odoo 8 ORM api , how to get results in reverse order using search()?

Nathan picture Nathan · Sep 30, 2015 · Viewed 11.1k times · Source

I try to use search() to fetch data from a table in a http controller.

 x = obj.search(cr, uid, criteria, offset=0,limit=36,order=sortBy)

It returns an array containing the ids of the top 36 items ordered by sortBy but always in increasing order. But how to make it using decreasing order?

Answer

Search

Takes a search domain, returns a recordset of matching records. Can return a subset of matching records (offset and limit parameters) and be ordered (order parameter):

Syntax:

search(args[, offset=0][, limit=None][, order=None][, count=False])

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: Returns records matching the search criteria up to limit.

Raise AccessError: if user tries to bypass access rules for read on the requested object.

You just need to search in following manner with descending order.

    sortBy = "field_name desc"
     x = obj.search(cr, uid, criteria, offset=0,limit=36,order=sortBy)

    ###Or you can define directly
     x = obj.search(cr, uid, criteria, offset=0,limit=36,order='field_name desc')