Odoo Help


6 Answers
31‏/12‏/2013 4:21 م

Well I finally got something working. This is what I did, I used the database cursor and wrote an sql select statement like this:

cr.execute('select "id" from "table_name" order by "id" desc limit 1')

id_returned = cr.fetchone()

the fetchone() method returns a tuple of one value which is assigned to the id_returned variable. I can then get the id from the tuple by referencing the first index like this: id_returned[0]


Atul Arvind

--Atul Arvind--
| 4 3 7
Vadodara, الهند
--Atul Arvind--


I am odoo Enthusiast/Developer from Vadodara, India. Working with odoo from v5 to v11 and other opensource technology since 5+ years. Having Strong Expertise in

Odoo Development

Odoo Customization

Odoo Training

Odoo Integration

Server Administration

Odoo Migration


Here is my StackOverflow profile.  you can also find me on LinkedIn.

Contact: atul.surficle@gmail.com.

Atul Arvind
1‏/1‏/2014 1:43 ص

If you don't want to execute PSQL query then you can search the record and get the max number, eg.

search_ids = self.pool.get("res.partner").search(cr, uid, [])
last_id = search_ids and max(search_ids)
19‏/3‏/2017 4:33 ص

Not quite sure about the older versions but you can do it like this in the newer ones:

last_id = self.env['table.name'].search([])[-1].id
1 Comment
Andreas Stange
7‏/8‏/2018 2:52 م

The ordering of the table might be such that the highest id is not equivalent to the last entry in the list. Thus I would explicitly provide the order in the search method:

last_id = self.env['table.name'].search([], order='id desc')[0].id

Rich Granata
2‏/1‏/2014 2:30 م

The sequencing is also stored in the database and can be retrieved with a statement like:

select * from res_partner_id_seq

Olivier DUCROT
10‏/11‏/2017 5:14 ص

you can use something like this :


the solution like the above does not work. It returns the id of the last one in the search() method, but the ids are not sorted. The last id should not be the max one.

last_id = self.env['table.name'].search([])[-1].id
Ask a Question
Keep Informed
1 follower(s)
About This Community

This platform 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.

Odoo Training Center

Access to our eLearning platform and experience all Odoo Apps through learning videos, use cases and quizzes.

Test it now