Odoo Help

Welcome!

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.

0

How to force commit in a loop

By
Jesse Rye
on 10/19/16, 4:16 PM 241 views

I have a button that when clicked imports records from an external source. It then loops through the records and uses .create to create the new records on the model in the database from the imported records. However it doesn't seem to commit them until its completed the loop of all imported records. Is there anyway to force the commit after each iteration?

object.create( template )
0
Jesse Rye
On 10/20/16, 10:27 AM

While Sah's answer appears like it should work I did some more research and found a much simpler answer for my scenario.

All I had to do was use the current environment variable and cursor and run commit(). See below.

self.env.cr.commit()
0
Sah Vinay Mahabir
On 10/20/16, 7:55 AM

add this to py file
#top of the file
from odoo import api, fields, models, _, modules
from odoo.http import request
#before for loop
r = modules.registry.RegistryManager.get(dbname)
cr = r.cursor()
#inside loop
cr.commit()
#make sure you close cr at the end of the function scope
cr.close()

Let me know if there is any error with your log file

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)

Stats

Asked: 10/19/16, 4:16 PM
Seen: 241 times
Last updated: 10/20/16, 10:38 AM