1👍
You can access the fields from the instance saved:
@receiver(post_save, sender=User)
def send_new_user_data_email(sender, instance, created, **kwargs):
if created:
username = instance.username
subject = 'Subject line'
message = f'User: {instance.username}\nPassword: ... \nblablabla'
send_mail(
subject,
message,
None,
[instance.email],
fail_silently=False,
)
In fact you do not need to save the instance first, you can send the email before saving it to the database:
from django.db.models.signals import pre_save
@receiver(pre_save, sender=User)
def send_new_user_data_email(sender, instance, created, **kwargs):
# …
pass
The email
field of Django’s builtin user model is not required (it has blank=True
), so it is possible that the user is created without an email address. If you want to alter that, you should make a slight modification to the form used by the ModelAdmin
for that User
model.
Source:stackexchange.com