Python 如何在Django中将@classmethod作为芹菜任务运行?
如何在Django中将@classmethod作为芹菜任务运行?下面是示例代码:Python 如何在Django中将@classmethod作为芹菜任务运行?,python,django,celery,Python,Django,Celery,如何在Django中将@classmethod作为芹菜任务运行?下面是示例代码: 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
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')
你可以像这样用芹菜把它包起来
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项目中执行芹菜配置的第一步,如前所述。这是否回答了您的问题?