Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update Django model object once Celery task is complete

I would like to update my model object once a celery task has completed. I am currently at a loss about how to go about doing this though.

Here is a layout of the files

models.py

from photos.tasks import photo_download

class Photo(models.Model):
    ....fields....


@receiver(post_save)
def download_photo_callback(sender, **kwargs):
    photo = kwargs["instance"]
    result = photo_download.delay(photo.uid)

tasks.py

from photo.models import Photo

@task()
def photo_download(photo_uid, callback=None):
    ...do stuff...
    photo.status = 'D'
    photo.save()
like image 558
Alexis Avatar asked Apr 24 '12 20:04

Alexis


2 Answers

You're doing a circular import. Your tasks.py file is importing your models.py file and vice-versa. You should move your signals to a separate signals.py file to avoid it.

like image 106
Sam Dolan Avatar answered Oct 18 '22 04:10

Sam Dolan


There is an example in documentation:

http://docs.celeryproject.org/en/latest/userguide/tasks.html#example

See spam_filter task:

http://docs.celeryproject.org/en/latest/userguide/tasks.html#blog-tasks-py

like image 31
bmihelac Avatar answered Oct 18 '22 04:10

bmihelac