Django Signals - pre_delete()


Django tip:

To notify another part of the application before the delete event of an object happens, you can use the pre_delete signal.

For example:

from django.db.models.signals import pre_delete
from django.dispatch import receiver


@receiver(pre_delete, sender=Inventory)
def allow_inventory_delete_if_no_order(sender, instance, **kwargs):
    if Order.objects.filter(inventory_item=instance.id).count() > 0:
        raise Exception("There are orders for this item.")