6. Signals¶
Django ships with the following signals for database operations:
- Saving a single instance:
pre_saveandpost_save - Deleting data:
pre_deleteandpost_delete - Changing a many to many relationship:
m2m_changed
Missing from this list is the ability to attach signals before and after updating data and bulk-creating data.
6.1. Bulk-create signals¶
The following signals are fired when data is created in bulk:
pre_bulk_createis fired just before data is createdpost_bulk_createis fired just after data is created
For copying data into the database from a buffer (i.e., using copy_from_instances):
pre_copy_from_instancesis fired just before data is copiedpost_copy_from_instancesis fired just after data is copied
6.2. Update signals¶
There are three sets of signals attached to the three ways you can update data.
For homogeneous updates (i.e., use the classic update()):
pre_updateis fired just before data is updatedpost_updateis fired just after data is updated
For heterogeneous updates (i.e., using update_fields()):
pre_update_fieldsis fired just before data is updatedpost_update_fieldsis fired just after data is updated
See Siganls Reference for more details.