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
.
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
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
File details
Details for the file django-soft-delete-1.0.15.tar.gz
.
File metadata
- Download URL: django-soft-delete-1.0.15.tar.gz
- Upload date:
- Size: 12.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 36cf26a9eaa5f4c0fdb5cb6367ea183e91b7f73783cad173e4071a4747dd1277 |
|
MD5 | 93f2c25efa218832f2ff33252385fef0 |
|
BLAKE2b-256 | 2102d2eac0e24abc6dcf5e06c73ed7ea44391482a85c13da30a1b4149ad3e005 |
File details
Details for the file django_soft_delete-1.0.15-py3-none-any.whl
.
File metadata
- Download URL: django_soft_delete-1.0.15-py3-none-any.whl
- Upload date:
- Size: 10.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fc16c870020984b7f58254adead12fdfb637a6c2f4bd8a93a3a636b18b1463e0 |
|
MD5 | 5f5c462aba4a8cff64434a89b76372f3 |
|
BLAKE2b-256 | d5799ea1d4a7a4a2d7a50c03036bce712adc37743b927a56cad0c0e8ce78395b |