Supercharging Django Productivity

Logical Deletion

One of the features we wanted from the beginning was a default policy against deleting anything from the database. It helps with reversing mistakes and separates active data from stuff we want to keep but don’t want to see. The challenge in such a system is how to reliably keep all the objects that were soft-deleted out of our queries.

A boolean is_void flag on our base model makes the magic happen. All of our querysets exclude this flag by default, but they can be accessed by usingModel.objects.all_objects_including_void() (a purposefully long name). On the model and queryset we override the delete() method to instead set theis_void flag to true and void any dependents. Hard deleting behavior lives atpurge_from_db(), making it obvious to developers what is happening.