Django - Executing a task through celery from a model

后端 未结 3 1387
时光取名叫无心
时光取名叫无心 2021-02-06 03:50

In my models.py:

from django.db import models
from core import tasks

class Image(models.Model):
    image     = models.ImageField(upload_to=\'images/orig\')
            


        
3条回答
  •  孤城傲影
    2021-02-06 04:39

    You don't need to import the task itself. Try using the following

    from django.db import models
    from celery.execute import send_task, delay_task
    
    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)
           result = delay_task("task_prefix.create_thumbnail", post.id)
    

提交回复
热议问题