---
title: "django-safedelete | pypx"
description: "Mask your objects instead of deleting them from your database."
canonical_url: "https://pypx.app/packages/django-safedelete"
meta:
  "og:description": "Mask your objects instead of deleting them from your database."
  "og:image": "https://pypx.app/_og/d/h_630%2Cc_PackageCard%2Cname_django-safedelete%2Cversion_1.5.0%2Csummary_Mask%2Byour%2Bobjects%2Binstead%2Bof%2Bdeleting%2Bthem%2Bfrom%2Byour%2Bdatabase.%2Clicense_BSD%2BLicense%2Cp_Ii9wYWNrYWdlcy9kamFuZ28tc2FmZWRlbGV0ZSI%2Cs_KwPqWOKz9KAZzv_6.png"
  "og:image:height": "630"
  "og:image:type": image/png
  "og:image:width": "1200"
  "og:title": "django-safedelete 1.5.0"
  "twitter:image": "https://pypx.app/_og/d/h_630%2Cc_PackageCard%2Cname_django-safedelete%2Cversion_1.5.0%2Csummary_Mask%2Byour%2Bobjects%2Binstead%2Bof%2Bdeleting%2Bthem%2Bfrom%2Byour%2Bdatabase.%2Clicense_BSD%2BLicense%2Cp_Ii9wYWNrYWdlcy9kamFuZ28tc2FmZWRlbGV0ZSI%2Cs_KwPqWOKz9KAZzv_6.png"
---

> Index of every page on this site: [llms.txt](https://pypx.app/llms.txt)

# **django-safedelete**

v1.5.0

Mask your objects instead of deleting them from your database.

48 KBpy3-none-anyBSD License2 deps

[** Diff **](https://pypx.app/packages/django-safedelete/diff)

`$ uv add django-safedelete`

# Django safedelete

## What is it ?

For various reasons, you may want to avoid deleting objects from your database.

This Django application provides an abstract model, that allows you to transparently retrieve or delete your objects, without having them deleted from your database.

You can choose what happens when you delete an object :

- it can be masked from your database (SOFT\_DELETE, the default behavior)
- it can be masked from your database and mask any dependent models. (SOFT\_DELETE\_CASCADE)
- it can be normally deleted (HARD\_DELETE)
- it can be hard-deleted, but if its deletion would delete other objects, it will only be masked (HARD\_DELETE\_NOCASCADE)
- it can be never deleted or masked from your database (NO\_DELETE, use with caution)

## Example

```python
# imports
from safedelete.models import SafeDeleteModel
from safedelete.models import HARD_DELETE_NOCASCADE

# Models

# We create a new model, with the given policy : Objects will be hard-deleted, or soft deleted if other objects would have been deleted too.
class Article(SafeDeleteModel):
    _safedelete_policy = HARD_DELETE_NOCASCADE

    name = models.CharField(max_length=100)

class Order(SafeDeleteModel):
    _safedelete_policy = HARD_DELETE_NOCASCADE

    name = models.CharField(max_length=100)
    articles = models.ManyToManyField(Article)


# Example of use

>>> article1 = Article(name='article1')
>>> article1.save()

>>> article2 = Article(name='article2')
>>> article2.save()

>>> order = Order(name='order')
>>> order.save()
>>> order.articles.add(article1)

# This article will be masked, but not deleted from the database as it is still referenced in an order.
>>> article1.delete()

# This article will be deleted from the database.
>>> article2.delete()
```

## Compatibilities

- Branch 0.2.x is compatible with django >= 1.2
- Branch 0.3.x is compatible with django >= 1.4
- Branch 0.4.x is compatible with django >= 1.8
- Branch 0.5.x is compatible with django >= 1.11
- Branch 1.0.x, 1.1.x and 1.2.x are compatible with django >= 2.2
- Branch 1.3.x is compatible with django >= 3.2 and Python >= 3.7

Current branch (1.3.x) is tested with :

- Django 3.2 using python 3.7 to 3.10.
- Django 4.0 using python 3.8 to 3.10.
- Django 4.1 using python 3.8 to 3.10.
- Django 4.2 using python 3.8 to 3.11.

## Installation

Installing from pypi (using pip).:

```
pip install django-safedelete
```

Installing from github.:

```
pip install -e git://github.com/makinacorpus/django-safedelete.git#egg=django-safedelete
```

Add safedelete in your INSTALLED\_APPS:

```python
INSTALLED_APPS = [
    'safedelete',
    [...]
]
```

The application doesn't have any special requirement.

## Configuration

In the main django settings you can activate the boolean variable SAFE\_DELETE\_INTERPRET\_UNDELETED\_OBJECTS\_AS\_CREATED. If you do this the update\_or\_create() function from django's standard manager class will return True for the created variable if the object was soft-deleted and is now "revived".

By default, the field that indicates a database entry is soft-deleted is deleted, however, you can override the field name using the SAFE\_DELETE\_FIELD\_NAME setting.

## Documentation

The documentation is available [here](http://django-safedelete.readthedocs.org). Generate your own documentation using:

tox -e docs

## Licensing

Please see the LICENSE file.

## Contacts

Please see the AUTHORS file.

=========

# CHANGELOG

# 1.5.0 (unreleased)

- Fix #242 - SafeDeleteQueryset.update() now applies the soft-delete

visibility filter before delegating to super().update(). Upstream's bulk-update path silently wrote to soft-deleted rows.

- Fix #247 - SafeDeleteAdmin.log\_undeletion dispatches by Django

version. Django 6 removed the single-object LogEntry.log\_action API in favour of log\_actions(..., queryset, single\_object=True).

- is\_safedelete\_cls now compares base.\_\_module\_\_ against this

module's own \_\_name\_\_ rather than the hard-coded literal 'safedelete.models'. The literal check silently disabled SOFT\_DELETE\_CASCADE whenever the package was imported under a non-default dotted path; the new check is location-agnostic.

- test\_prefetch\_related now resolves assertQuerySetEqual with a

nested getattr() so Django 5.1+ (which removed the lowercase assertQuerysetEqual alias) does not trip over the fallback arg.

- New test module safedelete/tests/test\_update\_bypass.py pinning the

#242 regression.

- New test module safedelete/tests/test\_admin\_django6.py pinning the

#247 regression across Django 5.x and 6.x.

# 1.4.1 (2025-03-05)

- Adding check for nullity in related models delete #243

# 1.4.0 (2024-05-13)

- Support Python 3.11 / Django 5.0
- Drop support for Django 3.2 / 4.0 / 4.1

# 1.3.3 (2023-11-13)

- Add Hard Delete Action to SafeDeleteAdmin #236

# 1.3.2 (2023-04-05)

- Fix undelete count in admin #220
- Fix de translation
- Fix argument type passed to db\_for\_write #222
- Django 4.2 support #223
- Fix SafeDeleteQueryset.as\_manager() now returns SafeDeleteManager

# 1.3.1 (2022-10-03)

- Fix exclude exception #216

# 1.3.0 (2022-08-17)

- Add typings #213
- Drop support for Django < 3.2 and Python 3.6
- Add an index for deleted field #153

# 1.2.3 (2022-08-17)

- Implement NO\_DELETE and HARD\_DELETE for queries #209
- Soft-delete-cascade raises an exception when trying to delete a object that related object is PROTECT #210

# 1.2.2 (2022-05-03)

- Fix soft delete cascade for generic relations #207
- Add count return to delete and undelete methods #204

# 1.2.1 (2022-04-29)

- Fix related\_objects for non-safedelete models.

# 1.2.0 (2022-04-26)

- Adding SafeDeleteCascadeControlModel #197
- Primary key uniqueness not considered in update\_or\_create #200

# 1.1.2 (2021-12-16)

- Add django 4.0 and python 3.10 compatibility and tests #191
- Fix highlight\_deleted\_field translations #183

# 1.1.1 (2021-11-15)

- Add highlight\_deleted\_field #177
- Add new safedelete admin filter #175
- Bug: has\_unique\_fields() returns false for models.UniqueConstraint #179

# 1.1.0 (2021-09-23)

- Fix attribute error when union queryset of safe delete model with others #152
- SET related objects after deleting #156
- Avoiding recursive call for delete function. #121
- Add SAFE\_DELETE\_FIELD\_NAME setting #164
- Move filter visibility logic to Query to fix subquery in annotations #160
- Add functions to customize the policies delete behavior #167

# 1.0.0 (2021-02-15)

- Drop support for Django < 2.2
- Fix deprecation warnings for Django 4.0

# 0.5.7 (2021-01-28)

- Add visibility filter for union operations #145
- Set related fields when soft deleted #144
- has\_unique\_fields() now takes into account unique\_together option #147
- Fix filtering in subqueries #148

# 0.5.6 (2020-07-20)

- call self.save() instead of super in delete()

# 0.5.5 (2020-05-14)

- Admin action now warns if some objects weren't undeleted
- Remove some pre-Django 1.11 support

# 0.5.4 (2019-12-02)

- Django 3.0 compatibility

# 0.5.3 (2019-12-02)

- Fix "Doing a .get on a queryset adds deleted objects #131"

# 0.5.2 (2019-08-19)

- Fix performance issue with Django 2.2
- Fix executing soft delete on already soft-deleted items during cascade soft delete

# 0.5.1 (2018-07-02)

- Fix possible unicode error in admin

# 0.5.0 (2018-05-29)

- Remove support for Django 1.8 to 1.10 and Python 3.3.

(it should still works for now but isn't tested, use at your own risks).

- Fix update\_or\_create (#98)

# 0.4.5 (2018-01-31)

- Fix an issue with Django 1.8 and `values_list`
- Django 2.0 compatibility

# 0.4.4 (2018-01-09)

**Bugfixes**

- Fix latest and earliest

# 0.4.3

**Bugfixes**

- Set SafeDeleteMixin as abstract

# 0.4.2

**Bugfixes**

- iterator() now filter the deleted objects correctly.
- Fix prefetch\_related() with all()
- Fix: "Cannot filter a query once a slice has been taken" error.

**Refactoring**

- Resolve Django 1.9+ allow\_tags deprecation warning
- Fix docstring typo in SafeDeleteManager, SOFT\_DELETE should be DELETED\_INVISIBLE

# 0.4.1

**New**

- Filtering on the deleted field is done on evaluation.
- Added specific managers: all\_objects and deleted\_objects.
- Added 'force\_policy' feature to SafeDeleteQuerySet.

**Removed**

\-

**Bugfixes**

- Fixed abstract intermediate models not working with SOFT\_DELETE\_CASCADE

**Refactoring**

- Renamed SafeDeleteMixin to SafeDeleteModel to better reflect the intended behavior. Using SafeDeleteMixin now throws a DeprecationWarning.
- Moved SafeDeleteQueryset

# 0.4.0

**New**

- deleted is now a datetime.

**Refactoring**

- Globals (HARD\_DELETE, SOFT\_DELETE, ...) have been moved `to config.py`.
- Removed support for Django 1.4 to 1.7. You should use the 0.3.x branch if you need to use safedelete in Django <= 1.7.
- Remove factories to use mixins instead.

# 0.3.5

**New**

- Change DELETED\_VISIBLE\_BY\_PK to DELETED\_VISIBLE\_BY\_FIELD to be able to change the field used.

# 0.3.4

**New**

- Add a SOFT\_DELETE\_CASCADE policy which perform a SOFT\_DELETE on safedelete related objects
- Django 1.8 compatibility

# 0.3.2

- Prevent migration errors on django 1.8 by declaring the SafeDeleteManager (internal class in managers) as global

# 0.3.1

- Fix issue with release on pypi not being the good one

# 0.3.0

**New**

- Add a NO\_DELETE policy which never soft or hard deletes an instance
- Django 1.8 compatibility

**Removed**

- Support of Django 1.2 and Django 1.3 has been removed

**Bugfixes**

- all\_with\_deleted method doesn't lose current queryset on a related manager
- uniqueness is now checked against soft deleted instances too
- prefetch\_related() now works with SafedeleteQuerySet
- Fix the undelete action in the administration for active objects.

# 0.2.1 (2014-12-15)

**New**

- Extends Django compatibility to Django 1.2

# 0.2.0 (2014-12-09)

**New**

- Django compatilibty 1.3 => 1.7
- Add administration utilities

## **Details **

<dl>

<dt>Version</dt>
<dd>1.5.0</dd>

<dt>License</dt>
<dd>BSD License</dd>

<dt>Maintainer</dt>
<dd>Korantin Auguste</dd></dl>

## **Links**

- [Download](https://github.com/makinacorpus/django-safedelete/tarball/1.5.0)
- [Homepage](https://github.com/makinacorpus/django-safedelete)
- [View on PyPI](https://pypi.org/project/django-safedelete/)

## **Release Cadence **

**1**

releases in the past year

avg 120 days between releases

## **Maintainers **

- Korantin Auguste · [contact@palkeo.com](mailto:contact@palkeo.com)

```json
{"@context":"https://schema.org","@graph":[{"@id":"https://pypx.app/#website","@type":"WebSite","description":"The Python Package Index, reimagined. Fast search, dependency insights, download trends, and changelogs — all in one place.","name":"pypx","url":"https://pypx.app/"},{"@id":"https://pypx.app/packages/django-safedelete#webpage","@type":"WebPage","description":"Mask your objects instead of deleting them from your database.","name":"django-safedelete","url":"https://pypx.app/packages/django-safedelete","isPartOf":{"@id":"https://pypx.app/#website"},"potentialAction":[{"@type":"ReadAction","target":["https://pypx.app/packages/django-safedelete"]}]},{"@id":"https://pypx.app/packages/django-safedelete#/schema/software-application/1","@type":"SoftwareApplication","applicationCategory":"DeveloperApplication","description":"Mask your objects instead of deleting them from your database.","license":"BSD License","name":"django-safedelete","softwareVersion":"1.5.0"}]}
```

## Sitemap

See the full [sitemap](https://pypx.app/sitemap.md) for all pages.
