Well, I have many operations inside my function. there is a loop which has a self.create()
function inside, and inside it there is still another loop containing another create
function and so on... BUT they depends on each other with their ID.
Here is a sample of code (minified)
@api.multi
def create_report(self):
id_report = None
reports = [ResumeReport(resume) for resume in data] # a tab containing many reports to be created
repo = self.env["module1"].search([("date", "=", str(date))])
if repo:
for r in repo:
id_report = r
else:
id_report = self.env["module1"].create({
'name': name
})
for rep in report:
self.env["module2"].create({
'report_id': id_report.id,
'name': name
})
So what I want to do is to put this code under a transaction. It will be a pretty big SQL operation, so when an error occurs, I want it to be rollbacked and cancel all operations (Transaction!). Otherwise to commit it. But I don't know if there is a possibility to do that in Odoo 10 as I had not found any doc about transaciton.
Could you help me please?
SOLVED
@api.multi
def create_report(self):
self._cr.autocommit(False)
try:
id_report = None
reports = [ResumeReport(resume) for resume in data] # a tab containing many reports to be created
repo = self.env["module1"].search([("date", "=", str(date))])
if repo:
for r in repo:
id_report = r
else:
id_report = self.env["module1"].create({
'name': name
})
for rep in report:
self.env["module2"].create({
'report_id': id_report.id,
'name': name
})
self._cr.commit()
except:
self._cr.rollback()