4

How to run @classmethod as a Celery task in Django? Here is example code:

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')
dev fan
  • 129
  • 7
  • Does this answer your question? [using class methods as celery tasks](https://stackoverflow.com/questions/9250317/using-class-methods-as-celery-tasks) – Huseyin Yagli Apr 09 '20 at 15:36

1 Answers1

5

You can wrap it in a celery task like this.

from celery import shared_task

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

and call it this way:

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

Don't forget to perform first steps of configuration of Celery in your Django project which is described here.

Majid
  • 638
  • 6
  • 21