当新用户注册后,我有以下信号可用于创建个人资料,然后向管理员发送电子邮件,通知他们新用户
@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)
我希望在配置文件更新时也通知管理员,但仍在努力提出解决方案。
有什么想法吗?
答案 0 :(得分:0)
方法-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
覆盖 save()
模型的 Profile
方法
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)