Django Celery send register email do not work

后端 未结 2 1491
终归单人心
终归单人心 2021-02-19 02:40

I am learning Celery. In my website, I let people register an account. Once they create a new account, it will automatically send an activation email to their user email address

2条回答
  •  死守一世寂寞
    2021-02-19 03:04

    Celery need to serialize the arguments for a task. Here, user is not serializable, but you can replace it with its ID:

    In tasks.py:

    from __future__ import absolute_import
    
    from celery.decorators import task
    from django.contrib.auth import get_user_model
    
    @task()
    def user_send_activation_email(user_id):
        user = get_user_model().objects.get(pk=user_id)
        user.send_activation_email()
    

    Then call it from views.py using:

    user_send_activation_email.delay(user_id=user.pk)
    

提交回复
热议问题