0

I have a model into I send an email each time this model is edited, with post_save, nice!

I can recover fields from my model to display them in the email, good.

But I would display information about the user connected, logged (username...). I tried some syntaxes, without success, please can you help me?

In model.py, at the end of my model related:

@receiver(post_save, sender=User)
def save(self, **kwargs):
    text = 'Hello\n%s %s has just been edited.' % (self.first_name, self.last_name)
    my_from = 'webmaster@hg-map.fr'
    my_subject = 'Prospect Edit (from test instance, home)'

    email = EmailMessage(my_subject, text, my_from, to=['xxx@xxx.fr'])
    email.send()
Georgie
  • 108
  • 11
  • oops...this is something weird. Seems like you have connected the *instance method* to signal. Is your `post_save` signal working? – JPG Nov 30 '19 at 14:22
  • Hello, yes it works as it. Why? You see some weird coding? Please tell me – Georgie Dec 02 '19 at 17:34
  • Mmmm... finally you right. It works good until... I restart the server... – Georgie Dec 06 '19 at 12:32

1 Answers1

0

Yes, now I do it, at the bottom of models.py:

@receiver(post_save, sender=MyModelRelated)
def save(sender, instance, **kwargs):
    text1 = 'Hello\n%s %s has just been edited.' % (instance.first_name, instance.last_name)
    form_admin_url = 'Admin form: https://xxx.fr/admin/prospect/prospect/'+str(instance.id)+'/change/'
    footer = 'This message was sent automatically, do not answer.'
    my_from = 'webmaster@xxx.fr'
    my_subject = 'Prospect Edited (%s %s)' % (instance.first_name, instance.last_name)

    email = EmailMessage(my_subject, text1+'\n'+form_admin_url+'\n'+footer, my_from, to=['geo@xxx.fr'])
    email.send()
Georgie
  • 108
  • 11