小编Gra*_*uff的帖子

在模型保存时在哪里调用 celery 任务

保存模型时我需要调用 celery 任务。我有冲突的导入,我不知道如何解决。我想知道是否有人知道我可以构建它以避免冲突的导入的另一种方法

models.py
from .tasks import celery_task

class Picture(PolymorphicModel):
    file = models.ImageField()
    processed_file = models.ImageField(blank=True, null=True)
    ...
    def save(self, *args, **kwargs):
        if self.file:
            self.processed_file = celery_task.delay(self.id, other_arg)
        super(Picture, self).save(*args, **kwargs)




tasks.py
from .models import Picture

@task
def celery_task(id, other_arg):
    try:
        picture = Picture.objects.get(id=id)
    except ObjectDoesNotExist:
        picture = None

    if picture:
        return some_other_function(picture.file)

    return None
Run Code Online (Sandbox Code Playgroud)

python django celery

0
推荐指数
1
解决办法
3906
查看次数

标签 统计

celery ×1

django ×1

python ×1