django-softdelete-it
Add soft-delete functionality to desired models.
Quick start
Follow steps mentioned below to add soft-delete feature in any model of your django app.
pip install django-softdelete-it
Add soft_delete_it to your INSTALLED_APPS setting like this:
INSTALLED_APPS = [ ... 'soft_delete_it', ]
Import SoftDeleteModel from soft_delete_it app to your model file like this:
from soft_delete_it import SoftDeleteModel
- Inherit SoftDeleteModel class to your model class. It will add following features:
- objects manager’s behavior will change such that:
delete() method which will soft delete instances
will always return return only ‘non soft deleted’ objects
hard_delete()` method to hard delete the objects
- all_objects manager:
will always return both soft deleted and non soft deleted objects
hard_delete() method to hard delete the objects
only_deleted() method to return only soft deleted objects
undelete() method to un-delete soft-deleted objects
Example
from django.db import models
from soft_delete_it.models import SoftDeleteModel
class Author(SoftDeleteModel):
name = models.CharField(max_length=50)
dob = models.DateField()
class Article(SoftDeleteModel):
title = models.CharField(max_length=50)
body = models.TextField(null=True)
author = models.ForeignKey(Author, on_delete=models.CASCADE, related_name='articles')
Bob = Author.objects.create(name='bob', dob='2000-12-12')
John = Author.objects.create(name='john', dob='1990-10-12')
Author.objects.all() # return QuerySet with 2 objects
Bob.delete() # Bob is soft-deleted
Author.objects.all() # return QuerySet with 1 object, John
Author.all_objects.all() # return QuerySet with 2 object, Bob and John
Bob.undelete() # un-deletes Bob object
Author.objects.all() # return QuerySet with 2 objects
article1 = Article(title='Bob The Builder', body='')
article1.author = Bob
article1.save()
Article.objects.all() # return QuerySet with 1 object, article1
Bob.delete() # soft-deletes both Bob and article1 as Article's author field is on_delete_cascade and it Inherits SoftDeleteModel
If you are implementing a new Manager for a model, simply inherit SoftDeleteManager as well along with other Managers.
- If you are implementing a new QuerySet for a model, you will need to do following:
Inherit SoftDeleteQuerySet
Write Manager inheriting SoftDeleteManager which defines soft-delete functionality in it’s __init__() method(as in the example) and override get_queryset() method(as in the example)
Write model class inheriting SoftDeleteModel and uses above new defined Manager method(as in the example)
Example with QuerySet
Lets create a QuerySet for Article such that if no author is provided while creating a new article, one default author will be added in object.
#Creating a default author object first
default_author = Author.objects.create(name='default')
#Implementing QuerySet
from soft_delete_it.models import SoftDeleteModel, SoftDeleteQuerySet, SoftDeleteManager
class ArticleQuerySet(SoftDeleteQuerySet):
def create(self, **kwargs):
try:
author = kwargs['author']
except KeyError:
kwargs['author'] = Author.objects.get(name='default')
article = super(ArticleQuerySet, self).create(**kwargs)
return article
class ArticleManager(SoftDeleteManager):
def __init__(self, *args, **kwargs):
self.deleted_also = kwargs.get('deleted_also', False)
super(ArticleManager, self).__init__(*args, **kwargs)
def get_queryset(self):
'''return all unsoft-deleted objects'''
if self.deleted_also:
return ArticleQuerySet(self.model)
return ArticleQuerySet(self.model).filter(deleted=None)
class Article(SoftDeleteModel):
title = models.CharField(max_length=50)
body = models.TextField(null=True)
author = models.ForeignKey(Author, on_delete=models.CASCADE, related_name='articles')
objects = ArticleManager.from_queryset(ArticleQuerySet)()
all_objects = ArticleManager.from_queryset(ArticleQuerySet)(deleted_also=True)
How soft-deletion functionality is implemented:
Create a new soft_delete app, whole code for soft-deletion functionality is implemented in its models.py file.
An abstract SoftDeleteModel added which contains a deleted attribute which is a UUIDField. It will hold None for undeleted object and a new uuid4 for deleted objects.
SoftDeleteQuerySet implemented to override default django’s delete method to soft-delete objects instead of hard deleting them.
undelete(), hard_delete(), only_deleted() methods are implemented in same QuerySet class to provide extra features.
SoftDeleteManger implemented to use above QuerySet by overriding get_queryset() method.
QuerySet’s delete method is necessary to override to support bulk_delete feature.
Call pre_delete and post_delete signals before and after the definition of above delete method.
Use NestedObjects from django admin utils to soft-delete all related objects.
Two managers, objects and all_objects to return undeleted, all objects are implemented.
Metadata
Release files for django-softdelete-it 0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django-softdelete-it-0.1.tar.gz | 6.6 kB | Without provenance | Details for django-softdelete-it-0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_softdelete_it-0.1-py3-none-any.whl | Python 3 | none | any | Details for django_softdelete_it-0.1-py3-none-any.whl |
Total release size: 14.9 kB
Release files / django-softdelete-it-0.1.tar.gz
| Download URL | django-softdelete-it-0.1.tar.gz |
|---|---|
| Size | 6.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ea4f39205a6f8c0dd8fc0686c1f937674ae4b8fed23e861cc7c193f66d533016
|
|
BLAKE2b-256 checksum How to use checksums |
5423db52ab9a513036d542d70bc9d68f453c3edd06377c1790551998798fc488
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / django_softdelete_it-0.1-py3-none-any.whl
| Download URL | django_softdelete_it-0.1-py3-none-any.whl |
|---|---|
| Size | 8.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d311b75435e87b742fd5210979f5b57973343c1fea4125d0d9728e9819dc5438
|
|
BLAKE2b-256 checksum How to use checksums |
2de492d806df8aa68a4181d8f88613ae17a02710a043a1d2a6f9b844920a9248
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for django-softdelete-it 0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.