3

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
  • Accessing Celery result in views is some wired you are trying to do, understand the concept of celery, I would suggest call that code in you view and use instead of celery. Commented Nov 16, 2018 at 6:31

1 Answer 1

5

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 %}
Sign up to request clarification or add additional context in comments.

1 Comment

Ended up using your answer to get me to a calculated @property tasks on the model, filtering on task_args = self.id thanx!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.