Managers¶
Built-in managers¶
-
class
safedelete.managers.
SafeDeleteManager
(queryset_class: Optional[Type[safedelete.queryset.SafeDeleteQueryset]] = None)[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
andDELETED_VISIBLE_BY_PK
. Defaults toDELETED_INVISIBLE
.>>> 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 fromSafeDeleteQueryset
.-
get_queryset
()[source]¶ Return a new QuerySet object. Subclasses can override this method to customize the behavior of the Manager.
-
all_with_deleted
() → django.db.models.query.QuerySet[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
() → django.db.models.query.QuerySet[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) → django.db.models.query.QuerySet[source]¶ Pass kwargs to
SafeDeleteQuerySet.all()
.- Args:
- force_visibility: Show deleted models. (default: {None})
Note
The
force_visibility
argument is meant for related managers when no other managers likeall_objects
ordeleted_objects
are available.
-
update_or_create
(defaults=None, **kwargs) → Tuple[django.db.models.base.Model, bool][source]¶ See
()
.Change to regular djangoesk function: Regular update_or_create() fails on soft-deleted, existing record with unique constraint on non-id field If object is soft-deleted we don’t update-or-create it but reset the deleted field to None. So the object is visible again like a create in any other case.
Attention: If the object is “revived” from a soft-deleted state the created return value will still be false because the object is technically not created unless you set SAFE_DELETE_INTERPRET_UNDELETED_OBJECTS_AS_CREATED = True in the django settings.
- Args:
- defaults: Dict with defaults to update/create model instance with kwargs: Attributes to lookup model instance with
-
Visibility¶
A custom manager is used to determine which objects should be included in the querysets.
-
class
safedelete.managers.
SafeDeleteManager
(queryset_class: Optional[Type[safedelete.queryset.SafeDeleteQueryset]] = None)[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
andDELETED_VISIBLE_BY_PK
. Defaults toDELETED_INVISIBLE
.>>> 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 fromSafeDeleteQueryset
.
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 theget()
orfilter()
function, passing it the default fieldpk
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.