In my models.py:
from django.db import models from core import tasks class Image(models.Model): image = models.ImageField(upload_to='images/orig') thumbnail = models.ImageField(upload_to='images/thumbnails', editable=False) def save(self, *args, **kwargs): super(Image, self).save(*args, **kwargs) tasks.create_thumbnail.delay(self.id)
In my tasks .py:
from celery.decorators import task from core.models import Image @task() def create_thumbnail(image_id): ImageObj = Image.objects.get(id=image_id)
This returns the following:
- Exception Type: ImportError
- Exception value: cannot import name tasks
The error will disappear if I comment from core.models import Image in tasks.py , however this will obviously cause a problem, since Image does not make sense here. I tried to import it inside create_thumbnail , but it still doesn't recognize Image .
I read somewhere that usually the object itself can be passed as an argument to the task, and this will solve my problem. However, one day a friend told me that it is considered best practice to send as little data as possible in a RabbitMQ message, so to achieve this I try to only pass the image ID and then return it to the task again.
1) Is what I'm trying to do is considered best practice? If so, how to do it?
2) I noticed in all the examples that I found on the Internet that they perform a task from a view and never from a model. I am trying to create a sketch whenever a new image is loaded, I do not want to call create_thumbnail in all the forms / views that I have. Any ideas on this? Does the task perform from a model that is not recommended or generally accepted?
django celery rabbitmq
abstractpaper
source share