Skip to main content

Soft delete models, managers, queryset for Django

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_soft_delete-1.0.20a3.tar.gz (17.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_soft_delete-1.0.20a3-py3-none-any.whl (14.8 kB view details)

Uploaded Python 3

File details

Details for the file django_soft_delete-1.0.20a3.tar.gz.

File metadata

  • Download URL: django_soft_delete-1.0.20a3.tar.gz
  • Upload date:
  • Size: 17.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for django_soft_delete-1.0.20a3.tar.gz
Algorithm Hash digest
SHA256 4ddc2db583e3c6ef8c0f6dc7f0698720f1f50593837196d329cbb3db990d2f74
MD5 1fcf0c53c9ad0a284abf125bac59e4f1
BLAKE2b-256 0265d591a346e4b0d81fcb1fb4d05e4c21cb86ff40ae8268642202ea59f320cb

See more details on using hashes here.

File details

Details for the file django_soft_delete-1.0.20a3-py3-none-any.whl.

File metadata

File hashes

Hashes for django_soft_delete-1.0.20a3-py3-none-any.whl
Algorithm Hash digest
SHA256 c04d2a3f23ba3b1d0d3b413e20f6a1d6f7ce58e96587e425dcba9347173cc023
MD5 c845fb57356a1859c45b7bd705a688b1
BLAKE2b-256 d76fd2d78df1503bcfc290326fe4924abdb696f5846553992454b8cf6ca72cd8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page