I have a model in Odoo to register some logs(Log)
and a helper class(Helper)
with async function to receive some data ad register on Log, this _job_function is called by a Cron Job like this:
class Log(models.Model): _name = 'saas.autobackup' _description = 'Description' field1 = fields.Char() field2 = fields.Char() field3 = fields.Datetime() def _job_function(self): helper = Hekper(self, ...) #Function receive self(Log) as parameter helper.run() #call functions
This is the helper class:
class Helper(): def __init__(self, og_obj) self.log_obj = log_obj #Receive the Log object as parameter async def some_async_func(self): (...) #Some async functions to get val1 and val2 self.create_log(val1,val2) def create_log(self, val1, val2) vals = {'field1': val1, 'field2': val1, 'field3': '2021-01-01'} self.log_obj.create(values) def run(self): loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) future = asyncio.ensure_future(self.some_async_func()) loop.run_until_complete(future)
For debugging propuser i have override the create class, to check if i’m sending wrong data. When the job is executed and the create_log function is called, the program correctly enters the create function, I also checked the data sent in create, and I verified that they were correct, just like the one I keep when trying to create within the Odoo interface itself. the “create” button. But inside Odoo by the create button, create normally and by the create_log function I get this error:
ValueError: <class 'AttributeError'>: "'tuple' object has no attribute 'cache'" while evaluating
Advertisement
Answer
As this is a threading problem, you need to make sure to create a new cursor.
def create_log(self, val1, val2) vals = {'field1': val1, 'field2': val1, 'field3': '2021-01-01'} with api.Environment.manage(): with registry(self.env.cr.dbname).cursor() as new_cr: new_env = api.Environment(new_cr, self.env.uid, self.env.context) self.with_env(new_env).log_obj.create(values)```