I want to send an email to a user on model change. Here is what I have : I have a model called package containing a field status and a field owner , I want to send an email to the owner of the package when the status is changed. Is there a way to do so ? Thank you
2 Answers
Signals might help you out.
Think of Signal methods as code that is always executed before or after an operation on a model; e.g, a pre_save()
signal is called just before a record is saved to database, post_delete()
is called just after a record is deleted from the database.
Implement a pre_save()
signal on the model and get the value of status
field from the database before saving. Check if this previous value is different from the one for the corresponding status field in update_fields
. If so, email the user.
Here are the docs for pre_save() and Signals.
Here's a tutorial I followed while learning Signals: How to Create Django Signals - SimpleIsBetterThanComplex.
Hope this helps :)

- 647
- 2
- 11
- 26
-
Follow Shuai Xu's method to send an email, in the `pre_save()` method. – Divij Sehgal Jul 10 '17 at 08:16
When the event of changing status is triggered, send the email to the owner.
Documentation: https://docs.djangoproject.com/en/1.11/topics/email/
Or if your prefer the built-in notification:How to use django-notification to inform a user when somebody comments on their post

- 58
- 8