如何在 Django 中将 运行 @classmethod 作为 Celery 任务?

How to run @classmethod as a Celery task in Django?

如何在 Django 中 运行 @classmethod 作为一个 Celery 任务?这是示例代码:

class Notification(TimeStampedModel):
    ....
    @classmethod
    def send_sms(cls, message, phone, user_obj=None, test=False):    
        send_message(frm=settings.NEXMO_FROM_NUMBER, to=phone, text=message)

        return cls.objects.create(
            user=user_obj,
            email="",
            phone=phone,
            text_plain=message,
            notification_type=constants.NOTIFICATION_TYPE_SMS,
            sent=True,
        )

n = Notification()
n.send_sms(message='test', phone='1512351235')

您可以像这样将其包装在 celery 任务中。

from celery import shared_task

@shared_task
def sample_task(message, phone):
  Notification.send_sms(message, phone)

并这样称呼它:

sample_task.delay(message='test', phone='1512351235')

不要忘记在 here.

中描述的 Django 项目中执行配置 Celery 的第一步