Built-in model

class safedelete.models.SafeDeleteModel(*args, **kwargs)[source]

Abstract safedelete-ready model.


To create your safedelete-ready models, you have to make them inherit from this model.

Attribute deleted:

DateTimeField set to the moment the object was deleted. Is set to None if the object has not been deleted.

Attribute _safedelete_policy:

define what happens when you delete an object. It can be one of HARD_DELETE, SOFT_DELETE, SOFT_DELETE_CASCADE, NO_DELETE and HARD_DELETE_NOCASCADE. Defaults to SOFT_DELETE.

>>> class MyModel(SafeDeleteModel):
...     _safedelete_policy = SOFT_DELETE
...     my_field = models.TextField()
>>> # Now you have your model (with its ``deleted`` field, and custom manager and delete method)
Attribute objects:

The safedelete.managers.SafeDeleteManager that returns the non-deleted models.

Attribute all_objects:

The safedelete.managers.SafeDeleteAllManager that returns the all models (non-deleted and soft-deleted).

Attribute deleted_objects:

The safedelete.managers.SafeDeleteDeletedManager that returns the soft-deleted models.

save(keep_deleted=False, **kwargs)[source]

Save an object, un-deleting it if it was deleted.

keep_deleted: Do not undelete the model if soft-deleted. (default: {False}) kwargs: Passed onto save().


Undeletes soft-deleted models by default.


Undelete a soft-deleted model.

kwargs: Passed onto save().


Will raise a AssertionError if the model was not soft-deleted.

delete(force_policy=None, **kwargs)[source]

Overrides Django’s delete behaviour based on the model’s delete policy.

force_policy: Force a specific delete policy. (default: {None}) kwargs: Passed onto save() if soft deleted.
class safedelete.models.SafeDeleteMixin(*args, **kwargs)[source]

SafeDeleteModel was previously named SafeDeleteMixin.

Deprecated since version 0.4.0: Use SafeDeleteModel instead.


You can change the policy of your model by setting its _safedelete_policy attribute. The different policies are:

This policy will:
  • Hard delete objects from the database if you call the delete() method.

    There is no difference with « normal » models, but you can still manually mask them from the database, for example by using obj.delete(force_policy=SOFT_DELETE).


This policy will:

This will make the objects be automatically masked (and not deleted), when you call the delete() method. They will NOT be masked in cascade.


This policy will:

This will make the objects be automatically masked (and not deleted) and all related objects, when you call the delete() method. They will be masked in cascade.

This policy will:
  • Delete the object from database if no objects depends on it (e.g. no objects would have been deleted in cascade).
  • Mask the object if it would have deleted other objects with it.
This policy will:
  • Keep the objects from being masked or deleted from your database. The only way of removing objects will be by using raw SQL.