3

I've looked at questions like this one and a dozen others. But none of them seems to be working.

I've a shared_task like this one, which doesn't return anything:

@shared_task
def rename_widget(widget_id, name):
    w = Widget.objects.get(id=widget_id)
    w.name = name
    w.save()

I've tried self.request.id and current_task.request.id but they both returned None. My celery version is 5.0.4 and django version is 3.1.1. I'm using Rabbitmq as messenger.

Judy T Raj
  • 1,755
  • 3
  • 27
  • 41

1 Answers1

2

Seems like a setup issue, or how you're calling the task. Without knowing more of the context, it is hard to say--perhaps you need to bind the method? I've sketched that out that solution:

tasks.py

from celery import shared_task
from demoapp.models import Widget

    @shared_task(bind=True)
    def rename_widget(self, widget_id, name):
        print(self.request.id) 
        w = Widget.objects.get(id=widget_id)
        w.name = name
        w.save()

views.py or somewhere else:

from tasks import rename_widget

result = rename_widget.delay(1, 'new_name')

If that's not the issue, I'd check out the full working Django example setup for ideas, found here: https://github.com/celery/celery/tree/master/examples/django/

gregory
  • 10,969
  • 2
  • 30
  • 42