I have a common field is_active = models.BooleanField()
on all my models.
I want to create a universal override to the delete function so that instead of removing records from the database, is_active = False
.
I understand the best way to do this is a pre_delete
signal rather than overriding delete()
itself as delete()
is not called in bulk operations.
I have tried the following implementation:
@receiver(pre_delete)
def delete_obj(sender, instance, **kwargs):
"""Override delete() to set object to inactive."""
return instance.is_active == False
However this still results in objects being deleted from the database. I assume this is because delete()
is still called after pre_delete
. How do I correct this?
From the docs:
Note that the delete() method for an object is not necessarily called when deleting objects in bulk using a QuerySet or as a result of a cascading delete. To ensure customized delete logic gets executed, you can use pre_delete and/or post_delete signals.