Odoo Help


How to get the id of a new record

Simon Lee
on 11/8/16, 1:00 AM 2,275 views

I am using odoo 9. When a new row is added in a tree view and before the "Save" button is clicked, is there any way I can get the id of this new row/record. self.id always returns False, it is of type NewId.

If this is not possible, is there anyway I can differential a row from another in the same tree view. For example, there are 2 new rows/records created before clicking the "Save" button. How can I identify row 1 from row 2 if both id is NewId???

Can someone share codes on how to do this.


Nilmar Shereef

--Nilmar Shereef--
--Nilmar Shereef--

Python Developer. Odoo Techno Functional Consultant.

Nilmar Shereef
On 11/8/16, 1:53 AM

self.id is returning false because it is defined only after creation. An alternative idea to get the id before saving is find the last id of the models and calculate the next one.
For ex:
search_ids = self.pool.get('your.model').search(cr, uid, [])
last_id = search_ids and max(search_ids)

It gives the last id of that model, you will get the next id by simple addition by one. And I think you can't create all rows at a time. It is one by one process(In code).
I think it may help you

On 11/8/16, 2:01 AM


To get the Id of the new Record You can call Create function and call super the returning value will be the new id like

def Create(self,vals):

  res = Super.(Class_name,Self).create(vals)

#res is your new record id

#make your modification here  check the field valuesand comparisons here from vals and then return id

return res

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 E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)


Asked: 11/8/16, 1:00 AM
Seen: 2275 times
Last updated: 11/10/16, 10:52 AM