In my DJango application I'd like to implement a system so that when the
MyModel
is changed, an email is sent to that email address. I have a ModelForm on this Model of mine. How and how should I implement this?
- Should I override the
save()
method of my ModelForm and send the email there?- Should I try and try an trap some kind of model updated signal and send the email there — if so, how? What signal shoudl I be trapping?
What is a good way to check that the email was changed. This itself is a trivial thing to implement but I'd like the code to reside it it's rightful place.
Thanks
Checking if a field was changed: Django: When saving, how can you check if a field has changed?
score:1
I wouldn't follow the advice of the referenced SO question. It got 18 upvotes but it would appear upvotes aren't everything ;). Actually, it's probably just dated info (2009).
It's far better to use a pre_save
signal to do this, which requires absolutely zero changes to your model and therefore doesn't have any negative consequences such as the answer your referenced has.
Essentially, in your receiver method, you look up the instance object from the database. Since this is pre-save, the database hasn't been changed yet. As a result, you can then compare instance.some_field
with obj.some_field
and see if they're different.
@receiver(pre_save, sender=MyModel)
def send_email_if_changed(sender, instance, **kwargs):
try:
obj = MyModel.objects.get(pk=instance.pk)
except MyModel.DoesNotExist:
pass # It's new, so email hasn't technically changed, but you might want to do something else here.
else:
if not obj.email == instance.email: # Email has changed
# send email
score:2
You can use django-model-changes to do this without an additional database lookup:
from django.db import models
from django.dispatch import receiver
from django_model_changes import ChangesMixin
class MyModel(ChangesMixin, models.Model):
# your model
@receiver(pre_save, sender=MyModel)
def send_email_if_changed(sender, instance, **kwargs):
if 'email' in instance.changes():
# send email
Credit To: stackoverflow.com
Related Query
- Sending emails when the email address is changed
- Giving email account a name when sending emails with Django through Google Apps
- Sending emails when a user is activated in the Django admin
- How do you change the display name when sending emails via Amazon SES in Django?
- Invalid email address error when sending email via Django
- Django send email to the user when a specific field on a model has bean changed
- Django: Is there a way to keep the dev server from restarting when a local .py file is changed and dynamically loaded?
- Python/Django: sending emails in the background
- STARTTLS extension not supported by server - Getting this error when trying to send an email through Django and a private email address
- Reliable way to only get the email text, excluding previous emails
- How not to set the user's email address in django social-auth
- How to override the from address in django email (sent through Gmail)
- What is the best way to store password in database when API call requires sending of password in plain text?
- django-registration can't set the "from" email address for authentication email
- how to get the email address from auth_user table of requested user id in django 1.5?
- django simple history is not working when changed from the application
- Is it possible to add a header to the email django sends to admins when a 500 is generated?
- What are the advantages of using SendGrid Web API instead of SMTP as the transport mechanism for sending emails in Django?
- Postfix hangs when sending email
- Error when sending email through Sendgrid API
- Send confirmation email when changing the email in Django
- Why is Django sending the wrong email template?
- Django "Incorrect Padding" error when sending email with send_mail
- Sending email notifications to subscribers when new blog post is published in Wagtail
- (Djoser) Weird activation email when I update the user fields
- Sending emails after updating the model from the admin page (Django)
- Personalizations field error when sending multiple dynamic template emails in sendgrid
- Djoser not sending emails when default emails are overridden
- How to override the CharField max_length validation error message that will be responded when sending a post request via API?
- Django openid authentication issue when I changed the url domain of my site but kept the same database
More Query from same tag
- Django Tinymce file manager
- Facebook Share button with facebook javascript sdk in a django template for loop
- Django Error: TypeError: int() argument must be a string or a number, not 'BuildsTable'
- How to load static css into django template
- Python How to GET Image and then POST (via requests library)
- Access HDF files stored on s3 in pandas
- Detect whether Celery is available and running
- Django-forms: Raise forms.ValidationError not working
- Can't import models in tasks.py with Celery + Django
- Get django user session objects from user email/id
- Heroku build React app before deploying through Django
- How to see posts using functions based view in django?
- Reverse queryset/list, then slice
- How to call the value of a key in JavaScript?
- Character encoding issue when using Python3/mysqlclient to retreive data encoded in latin1