How to force commit inside a loop?

THE CHALLENGE:

I found myself creating and updating hundreds of rows of data in a loop. While this was working just fine I wasn’t seeing any of my new or updated data in Odoo until the loop had completed processing all the rows of data. There must be a way to force commit after each loop.

I posted the question on the Odoo forum and a user provided a great solution but it was a bit more complicated in lines of code than what I had hoped for. After a little bit more research I found the answer was very simple.

THE SOLUTION:

You need to retrieve the cursor object so you can run the commit() function on it. Simply use the self object’s environment variable to force commit. But be warned this can have dire effects if you aren’t handling your data properly per the Odoo guidelines.

self.env.cr.commit()
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s