콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
4 답글
30868 화면

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 )
아바타
취소
작성자 베스트 답변

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()
아바타
취소
베스트 답변
  • self.env.cr.commit() commits the transaction's buffered write operations.
  • self.env.cr.savepoint() sets a transaction savepoint to rollback to.
  • self.env.cr.rollback() cancels the transaction's write operations since the last commit, or all if no commit was done.


아바타
취소
베스트 답변

commit the code by 

self.env.cr.commit()

If you have too many records in for loop then commit every 100 or 1000th record

count += 1
if count == 100:
_logger.info('100 records created ')
self.env.cr.commit()
count = 0

아바타
취소
베스트 답변

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

아바타
취소
관련 게시물 답글 화면 활동
0
9월 19
98
1
7월 17
3641
4
8월 16
7464
1
5월 16
4450
1
3월 16
4458