Odoo Help


This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


env environment

Billiard R Made
on 1/11/16, 7:50 AM 421 views

How can I use

cus = self.env['res.partner'].search([('id', ...

to find the highest id in the table res.partner ?



| 4 3 5
Hyderabad, India

On 1/12/16, 12:55 AM


You can use:

self.env['res.partner'].search([ ], order="id desc" limit="1")

here order helps to get the id in descending order and limit helps you to fetch the no of records you want to fetch.. i kept 1 to fetch the highest/final id
Hope it helps you

self.env['res.partner'].search([ ], order="id desc", limit=1)

Billiard R Made
on 1/12/16, 2:01 AM


| 6 4 7
Tbilisi, Georgia
On 1/11/16, 9:57 AM

all_records_sorted_by_id = self.env['res.partner'].search([ ]).sorted(key = lambda rec: rec.id, reverse=True)
if all_records_sorted_by_id:
    highest_id = all_records_sorted_by_id[0].id

Now I get this error: TypeError: sorted() got an unexpected keyword argument 'reverse'

Billiard R Made
on 1/11/16, 11:37 AM

you should not receive this error if you write clean v8.0 code and "self" is a recordset in the above code (because of "self.env" used in the code, I assumed that it is v8, but it seems you mix v7/v8 styles and self is not a recordset here as you got this error... ) . see the definition of "sorted" function: "Odoo 8.0"/openerp/models.py#L5450 you can see that "sorted" function has the "reverse" keyword argument in it's definition. Anyway, @Pawan provided better solution (it avoids to turn around all partners list, so more suitable solution especially in case of large quantity of partners), but this one should work as well in a clean v8.0 API code.

on 1/12/16, 4:19 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)


Asked: 1/11/16, 7:50 AM
Seen: 421 times
Last updated: 1/12/16, 2:01 AM