如何在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')
答案 0 :(得分:1)
您可以将其包装在像这样的芹菜任务中。
from celery import shared_task
@shared_task
def sample_task(message, phone):
Notification.send_sms(message, phone)
并这样称呼:
sample_task.delay(message='test', phone='1512351235')
请不要忘记在您的Django项目中执行配置Celery的第一步,该过程已在here中进行了描述。