Access django celery results in views

Viewed 2010

I'm using the django-celery-results extension and successfully saving records in the db backend table, celery_results_taskresults. My tasks are associated with model instances, and I want to be able to list them as attributes of each instance, in views and ultimately templates. I can see them in the admin interface, but can't figure out how to access them in a list.

I though of creating an @property on the model in question, using raw sql, but the sql examples I've seen all refer to a model, and if there's a celery_results_taskresults model, I can't find it.

1 Answers

As celery_results_taskresults use a model to store results, so we can use them in the views. You can try like this:

from django_celery_results.models import TaskResult


class SomeTemplateView(TemplateView):

     def get_context_data(self, *args, **kwargs):
         context = super(SomeTemplateView, self).get_context_data(*args, **kwargs)    
         context['results'] = TaskResult.objects.all()
         return context

And in the template:

{% for r in results %}
      {{r.task_name}}
      ...
{% endfor %}
Related