更改用户密码后在 Django 模型中发送邮件的最佳方式?

Best way to send a mail in Django Model when a user's password has been changed?

我是 python 和 django 今年的新手,我只是在努力弄清楚如何在更新密码后通过 send_mail 向用户发送一封简单的邮件? 我已经通过 pre_save 的 Signals 对此进行了管理,但是我不想让用户等到邮件已发送(据我所知我无法解决)。使用post_save,无法查询到之前的状态。

如果我给出以下用户模型,这里最好的方法是什么?

class User(AbstractBaseUser):

    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    email = models.EmailField(verbose_name="email address", max_length=255, unique=True)
    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)
    USERNAME_FIELD = "email"
    REQUIRED_FIELDS = []

    # Tells Django that the UserManager class defined above should manage
    # objects of this type
    objects = UserManager()

    def __str__(self):
        return self.email

    class Meta:
        db_table = "login"

我用 pre_save 信号设置了它,但由于延迟,这对我来说不是解决方案:

@receiver(pre_save, sender=User)
def on_change(sender, instance: User, **kwargs):
    if instance.id is None:
        pass
    else:
        previous = User.objects.get(id=instance.id)
        if previous.password != instance.password:
            send_mail(
                "Your password has changed",
                "......",
                "info@examplpe.com",
                [previous.email],
                fail_silently=False,
            )

提前致谢

如果您使用的是自定义模型,您可能可以通过对 set_password() 的调用来在实例上设置标志,然后检测其在信号中的存在。

试试这个例子:

from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
from django.db.models.signals import post_save


class User(AbstractBaseUser, PermissionsMixin):
    
    ...
    
    def set_password(self, password):
        super(User, self).set_password(password)
        self._set_password = True

    @classmethod
    def user_changed(cls, sender, instance, **kwargs):
        if getattr(instance, '_set_password', False):
            # Send your mail


post_save.connect(User.user_changed, sender=User)

您可以覆盖用户模型的保存方法。这是来自 docs alongside a check for changed values from SO:

的示例
class User(AbstractBaseUser):
    ...

    __original_password = None

    def __init__(self, *args, **kwargs):
        super(User, self).__init__(*args, **kwargs)
        self.__password = self.password

    def save(self, *args, **kwargs):
        if self.password != self.__original_password:
            notify_user_of_password_change()
        super().save(*args, **kwargs)  # Call the "real" save() method.