Django SafeDelete is an active library (current version 1.4.1) that provides soft deletion functionality for Django models, allowing objects to be masked from the database instead of permanently deleted. This enables recovery of 'deleted' data and aids in auditing. It offers various deletion policies and integrates with Django's ORM and Admin interface. Releases occur periodically, with significant updates often tied to Django version compatibility.
pip install django-safedeleteVerified import paths — ran on the pinned version, not inferred.
To use django-safedelete, make your model inherit from `SafeDeleteModel` and optionally set a `_safedelete_policy`. By default, `SOFT_DELETE` is used. Objects are then soft-deleted by calling `.delete()` and can be queried using `Article.all_objects` or `Article.deleted_objects`. To undelete, use the `.undelete()` method. You can force a hard delete using `force_policy=SafeDeleteModel.HARD_DELETE`.
Update your models to inherit from `safedelete.models.SafeDeleteModel` instead of `SafeDeleteMixin`.
Adjust any code that checks `obj.deleted` to check `obj.deleted is not None` or `obj.deleted__isnull=False` in queries, to account for the `DateTimeField` behavior.
Always check the `django-safedelete` changelog or PyPI page for supported Django and Python versions before upgrading, especially if your project relies on older environments.
Set `_safedelete_visibility = DELETED_INVISIBLE` (the default for managers) on your managers or models, and understand that direct access can still bypass this. For stricter control, you might need to override managers for related fields or use `SafeDeleteQueryset.visible()` or `SafeDeleteQueryset.undeleted()` in your queries.
Be aware of this setting's effect when using `update_or_create()`. If you need to distinguish between a new creation and an undeletion, inspect the object's `deleted` field or use custom logic.