[Django]-Sending emails after updating the model from the admin page (Django)

5๐Ÿ‘

โœ…

Try overriding the ModelAdmin.save_model method. I think it has hooks for all the information you require.

The change variable lets you distinguish between a user adding or changing the model instance.

form.changed_data gives you a list of the names of the fields which have changed, which lets you determine whether or not to send the email.

Finally request.user identifies the user which made the changes.

๐Ÿ‘คAlasdair

1๐Ÿ‘

You need django.db.models.signals.post_save signal. It is sanding after the model has been saved.

def my_callback(sender, **kwargs):
    # Your specific logic here
    pass

post_syncdb.connect(my_callback, sender=yourapp.models.TheModel)

Arguments sent with this signal:

  • sender:
    The model class.
  • instance:
    The actual instance being saved.
  • created
    A boolean; True if a new record was created.
  • raw:
    A boolean; True if the model is saved exactly as presented (i.e. when loading a fixture). One should not query/modify other records in the database as the database might not be in a consistent state yet.

So you need only callback and sender.

๐Ÿ‘คI159

Leave a comment