gpt4 book ai didi

python - Django 配置文件更新现有配置文件

转载 作者:太空宇宙 更新时间:2023-11-03 21:47:36 25 4
gpt4 key购买 nike

当新用户注册时,我有以下信号来创建个人资料,然后向管理员发送电子邮件,通知他们新用户

@receiver(post_save, sender=User)
def create_user_profile(sender, **kwargs):
'''Create a profile for a new user'''
if kwargs['created']:
Profile.objects.create(user=kwargs['instance'])


@receiver(post_save, sender=User)
def notify_admin(sender, instance, created, **kwargs):
'''Notify the administrator that a new user has been added.'''
if created:
subject = 'New Registration created'
message = 'A new candidate %s has registered with the site' % instance.email
from_addr = 'no-reply@example.com'
recipient_list = ('admin@example.com', )
send_mail(subject, message, from_addr, recipient_list)

我还希望在个人资料更新时通知管理员,但正在努力想出解决方案。

有什么想法吗?

最佳答案

方法-1
正如 @Ralf 所说,对 Profile 模型使用不同的信号并检查 created 标志

@receiver(post_save, sender=Profile)
def profile_update_signal(sender, instance, created, **kwargs):
# Profile update signal
if not created:
# do something here <<<<<<
send_mail()



方法 - 2重写 Profile 模型的 save() 方法

class Profile(models.Model):
# your fields
def save(self, *args, **kwargs):
if self.pk: # not a new object
# do something here <<<<<<
send_mail()
super(Profile, self).save(*args, **kwargs)

关于python - Django 配置文件更新现有配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52374477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com