Skip to Content
Menu
This question has been flagged

In Odoo14, I have set up a cronjob that calls a method below:


from odoo.addons.web.controllers.main import Export
def get_export_fields_data(self):
	​export_fields = Export().namelist(self.model, self.export_id.id)
	return export_fields


When I try to run the cronjob manually, this works fine and returns the required data.

But, when the cronjob is automatically executed in the specified time, this code does not work and throws an error as below:

File "/home/odoo14/envodoo14/lib/python3.10/site-packages/werkzeug/local.py", line 447, in__get__obj= instance._get_current_object()  
File "/home/odoo14/envodoo14/lib/python3.10/site-packages/werkzeug/local.py", line 569, in _get_current_objectreturnself.__local() # type: ignore
File "/home/odoo14/envodoo14/lib/python3.10/site-packages/werkzeug/local.py", line 234, in _lookup
raise RuntimeError("object unbound")
RuntimeError: object unbound


While I debugged the issue, I found out that there is an error while accessing odoo.http.request in the odoo/addons/web/controllers/main.py file. There is some difference between request while running the cronjob manually versus executing automatically.


Has anyone faced this error?

Avatar
Discard
Best Answer
Accessing the odoo.http.request object in a cron job may cause your error. 
The odoo.http.request object is available during HTTP requests but not in cron jobs.

Don't use odoo.http.request object in cron jobs.
Refactor your code to explicitly send the data to the get_export_fields_data method instead of using the odoo.http.request object.

Here is example code:

from odoo.addons.web.controllers.main import Export
def get_export_fields_data(model, export_id):
export_fields = Export().namelist(model, export_id)
return export_fields
# Modify your cron job method to pass the required arguments explicitly
def your_cron_job_method(self):
model = 'your.model' # Replace with the actual model
export_id = 123 # Replace with the actual export ID
export_fields = get_export_fields_data(model, export_id)
# Rest of your cron job logic

By directly passing the appropriate arguments to the get_export_fields_data method, you can remove the dependency on the odoo.http.request object and make it compatible with manual execution and cron job execution.


Avatar
Discard
Related Posts Replies Views Activity
1
Dec 24
2224
1
Jun 24
959
1
May 24
2090
2
Feb 24
1767
1
Oct 23
1543