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 objectdeleted_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.
Installation
pip install django-soft-delete
Add the SoftDeleteModel as a parent for your model:
# For regular model
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()
# For inherited model
class Post(SoftDeleteModel, SomeParentModelClass):
title = models.CharField(max_length=100)
Make and apply the migrations:
./manage.py makemigrations
./manage.py migrate
Quick example
a1 = Article.objects.create(title='Django')
a2 = Article.objects.create(title='Django')
a3 = Article.objects.create(title='Django')
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.
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.
class YourOwnQuerySet(SoftDeleteQuerySet):
pass
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django-soft-delete-0.9.21.tar.gz.
File metadata
- Download URL: django-soft-delete-0.9.21.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
70ef192d1ad8be5ec30ca033783f199d61f2d1e1bd60ad8a7db1e8102552f23b
|
|
| MD5 |
f16b82a434267380d5fd904de76f25ae
|
|
| BLAKE2b-256 |
a86c9efc2e312b7821e1c406209a09de30bd50a3caf835c4f590ae96786287d2
|
File details
Details for the file django_soft_delete-0.9.21-py3-none-any.whl.
File metadata
- Download URL: django_soft_delete-0.9.21-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
32c5cc54f3a1990b13f0f5c66089f712a823363997749abc594112ac2c521a1c
|
|
| MD5 |
8d59fc33e6d95f4bb8566d83c7c693c0
|
|
| BLAKE2b-256 |
e66fcffd2e8f57334f1f5d2e3c5f362f3443973fa06730d328553929508f6452
|