Skip to main content

Django Soft Delete

This is a set of small classes to make soft deletion of objects.
Use the abstract model SoftDeleteModel for adding two new fields:

  • is_deleted - is a boolean field, shows weather of a deletion state of object
  • deleted_at - is a DateTimeField, serves a timestamp of deletion.

Also, you can use SoftDeleteManager and DeletedManager object managers for getting alive and deleted objects accordingly.

By default, the SoftDeleteModel has objects attribute as SoftDeleteManager and deleted_objects attribute as DeletedManager.

How to use

pip install django-soft-delete

Add the SoftDeleteModel as a parent for your model:

# For regular model
from django.db import models
from django_softdelete.models import SoftDeleteModel

class Article(SoftDeleteModel):
    title = models.CharField(max_length=100)
    
    # Following fields will be added automatically
    # is_deleted
    # deleted_at
    
    # Following managers will be added automatically
    # objects = SoftDeleteManager()
    # deleted_objects = DeletedManager()
    # global_objects = GlobalManager()


# For inherited model
from django_softdelete.models import SoftDeleteModel

class Post(SoftDeleteModel, SomeParentModelClass):
    title = models.CharField(max_length=100)

Make and apply the migrations:

./manage.py makemigrations
./manage.py migrate

Relations

You can also use soft deletion for models that are related to others. This library does not interfere with standard Django functionality. This means that you can also use cascading delete, but remember that soft delete only works with the SoftDeleteModel classes. If you delete an instance of the parent model and use cascading delete, then the instances of the child model will be hard-deleted. To prevent hard deletion in this case, you should use SoftDeleteModel for child models in the same way as for parent model.

Quick example

a1 = Article.objects.create(title='django')
a2 = Article.objects.create(title='python')
a3 = Article.objects.create(title='django_softdelete')
Article.objects.count()  # 3

a1.delete()  # soft deletion of object
Article.objects.count()  # 2

deleted_a1 = Article.deleted_objects.first()  # <Article: 'django'>
deleted_a1.restore()  # restores deleted object
Article.objects.count()  # 3
Article.deleted_objects.count()  # 0

a1.hard_delete()  # deletes the object at all.

Batch deletion

Article.objects.filter(some_value=True).delete()  # soft delete for all filtered objects
Article.deleted_objects.filter(some_value=True).restore()  # restore for all filtered objects

Custom manager

If you need a soft delete functionality for model with your own object manager, you want to extend it with the SoftDeleteManager.

from django_softdelete.models import SoftDeleteManager

class YourOwnManager(SoftDeleteManager):
    pass

The same class exists for the deleted_objects manager too -- DeletedManager.

Custom QuerySet

If you need to use soft delete functionality for your custom QuerySet, use the SoftDeleteQuerySet as a parent class or extending existing one.

from django_softdelete.models import SoftDeleteQuerySet

class YourOwnQuerySet(SoftDeleteQuerySet):
    pass

Support me with a cup of coffee

USDT (ERC20): 0x308dad9B7014AdeD217e817B6274EeeD971200F9

ko-fi

Metadata

Release files for django-soft-delete-condor 1.0.18.post1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-soft-delete-condor 1.0.18.post1
File Size Uploaded
django_soft_delete_condor-1.0.18.post1.tar.gz 12.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-soft-delete-condor 1.0.18.post1
File Interpreter ABI Platform
django_soft_delete_condor-1.0.18.post1-py3-none-any.whl Python 3 none any Details

Total release size: 23.0 kB

Release files / django_soft_delete_condor-1.0.18.post1.tar.gz

Download URL django_soft_delete_condor-1.0.18.post1.tar.gz
Size 12.0 kB
Tags Source
SHA-256 checksum
How to use checksums
e209f526a5c7b31fe96337c1cfb1fd5f2011337bdc3104e25189843981f4ef68
BLAKE2b-256 checksum
How to use checksums
6e3fff7b344573964246f7ae7f56f1620f0ef2e69f1038a04fa8a72772847fbf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.1

Release files / django_soft_delete_condor-1.0.18.post1-py3-none-any.whl

Download URL django_soft_delete_condor-1.0.18.post1-py3-none-any.whl
Size 11.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
de50de38aee53dc8333af242daf73d4940eca323737d79753147ca34d2617659
BLAKE2b-256 checksum
How to use checksums
88a3ca1f5f1dc9087403938c10283f309f39263a66061fb2f051413b5fc95719
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.1

Release history Release notifications | RSS feed

This release

1.0.18.post1 This release

2 release files

1.0.16

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page