Managers

Built-in managers

class safedelete.managers.SafeDeleteManager(queryset_class=None, *args, **kwargs)[source]

Default manager for the SafeDeleteModel.

If _safedelete_visibility == DELETED_VISIBLE_BY_PK, the manager can returns deleted objects if they are accessed by primary key.

Attribute _safedelete_visibility:
 

define what happens when you query masked objects. It can be one of DELETED_INVISIBLE and DELETED_VISIBLE_BY_PK. Defaults to SOFT_DELETE.

>>> from safedelete.models import SafeDeleteModel
>>> from safedelete.managers import SafeDeleteManager
>>> class MyModelManager(SafeDeleteManager):
...     _safedelete_visibility = DELETED_VISIBLE_BY_PK
...
>>> class MyModel(SafeDeleteModel):
...     _safedelete_policy = SOFT_DELETE
...     my_field = models.TextField()
...     objects = MyModelManager()
...
>>>
Attribute _queryset_class:
 

define which class for queryset should be used This attribute allows to add custom filters for both deleted and not deleted objects. It is SafeDeleteQueryset by default. Custom queryset classes should be inherited from SafeDeleteQueryset.

all_with_deleted()[source]

Show all models including the soft deleted models.

Note

This is useful for related managers as those don’t have access to all_objects.

deleted_only()[source]

Only show the soft deleted models.

Note

This is useful for related managers as those don’t have access to deleted_objects.

all(**kwargs)[source]

Pass kwargs to SafeDeleteQuerySet.all().

Args:
show_deleted: Show deleted models. (default: {False})

Note

The show_deleted argument is meant for related managers when no other managers like all_objects or deleted_objects are available.

class safedelete.managers.SafeDeleteAllManager(queryset_class=None, *args, **kwargs)[source]

SafeDeleteManager with _safedelete_visibility set to DELETED_VISIBLE.

Note

This is used in safedelete.models.SafeDeleteModel.all_objects.

class safedelete.managers.SafeDeleteDeletedManager(queryset_class=None, *args, **kwargs)[source]

SafeDeleteManager with _safedelete_visibility set to DELETED_ONLY_VISIBLE.

Note

This is used in safedelete.models.SafeDeleteModel.deleted_objects.

Visibility

A custom manager is used to determine which objects should be included in the querysets.

class safedelete.managers.SafeDeleteManager(queryset_class=None, *args, **kwargs)[source]

Default manager for the SafeDeleteModel.

If _safedelete_visibility == DELETED_VISIBLE_BY_PK, the manager can returns deleted objects if they are accessed by primary key.

Attribute _safedelete_visibility:
 

define what happens when you query masked objects. It can be one of DELETED_INVISIBLE and DELETED_VISIBLE_BY_PK. Defaults to SOFT_DELETE.

>>> from safedelete.models import SafeDeleteModel
>>> from safedelete.managers import SafeDeleteManager
>>> class MyModelManager(SafeDeleteManager):
...     _safedelete_visibility = DELETED_VISIBLE_BY_PK
...
>>> class MyModel(SafeDeleteModel):
...     _safedelete_policy = SOFT_DELETE
...     my_field = models.TextField()
...     objects = MyModelManager()
...
>>>
Attribute _queryset_class:
 

define which class for queryset should be used This attribute allows to add custom filters for both deleted and not deleted objects. It is SafeDeleteQueryset by default. Custom queryset classes should be inherited from SafeDeleteQueryset.

If you want to change which objects are “masked”, you can set the _safedelete_visibility attribute of the manager to one of the following:

safedelete.managers.DELETED_INVISIBLE

This is the default visibility.

The objects marked as deleted will be visible in one case : If you access them directly using a OneToOne or a ForeignKey relation.

For example, if you have an article with a masked author, you can still access the author using article.author. If the article is masked, you are not able to access it using reverse relationship : author.article_set will not contain the masked article.

safedelete.managers.DELETED_VISIBLE_BY_FIELD

This policy is like DELETED_INVISIBLE, except that you can still access a deleted object if you call the get() or filter() function, passing it the default field pk parameter. Configurable through the _safedelete_visibility_field attribute of the manager.

So, deleted objects are still available if you access them directly by this field.