Reputation: 2412
I'm using django_celery_results
to save results of some celery tasks. Each task gets kwargs
as input which at the end gets saved in task_kwargs
field of TaskResult
.
Im having trouble loading those kwargs
later from the way they get saved in DB. For example this is one entry:
"{'config_file_path': '/path/to/configs/some_config.json'}"
Simple example of accessing the field value:
tkwargs = TaskResult.objects.get(id=1).task_kwargs
for which i get the above string.
What is a straightforward way to get task_kwargs
as a python dictionary instead of that string?
Upvotes: 1
Views: 1010
Reputation: 549
Wish this were easier with this package (why aren't they deserialized out of the model?):
recent_tasks = TaskResult.objects.order_by('-date_done')[:10]
tasks_list = [
{
'task_id': task.task_id,
'task_name': task.task_name,
'task_args': json.loads((task.task_args or "null").replace("()", "null").replace('"null"', 'null')),
'task_kwargs': json.loads((json.loads(task.task_kwargs or 'null') or "null").replace("'", '"')),
'status': task.status,
'result': json.loads(task.result),
'date_created': task.date_created,
'date_done': task.date_done,
'meta': json.loads(task.meta),
'worker': task.worker,
'traceback': task.traceback,
}
for task in recent_tasks
]
Upvotes: 0
Reputation: 419
This is what I have:
args = json.loads(task.task_kwargs)
if isinstance(args, str):
args = args.replace("'", '"')
args = json.loads(args)
Not pretty, but it works.
Upvotes: 2