Django Soft Deletion 🚀
Django Soft Deletion is a Django-based library that enables soft deletion by using a deleted_at timestamp instead of permanently removing records. This allows you to mark objects as deleted while keeping them in the database.
📌 Features
✅ Soft Delete Models – Records are hidden, not deleted.
✅ Custom QuerySet – Easily filter active and deleted records.
✅ Works with Django ORM – No need for major changes in your models.
✅ Cascade Soft Delete Support – Automatically soft deletes related objects.
✅ Signal Support – Pre and post soft delete signals included.
📦 Installation
pip install git+https://github.com/alirafiei75/django-soft-deletion.git
🔧 Usage
1️⃣ Add SoftDeleteModel to Your Models
Extend SoftDeleteModel in your models to enable soft deletion:
from soft_deletion.models import SoftDeleteModel
class MyModel(SoftDeleteModel):
name = models.CharField(max_length=255)
2️⃣ Soft Delete an Object
Instead of permanently deleting an object, soft delete it:
obj = MyModel.objects.get(id=1)
obj.soft_delete()
3️⃣ Query Active or Deleted Objects
Use the built-in queryset filters:
# Get only active (non-deleted) records
MyModel.objects.active()
# Get only soft-deleted records
MyModel.objects.deleted()
⚡ Soft Delete Behavior with Foreign Keys
django-soft-deletion respects Django's on_delete behavior.
class RelatedModel(SoftDeleteModel):
name = models.CharField(max_length=255)
class MyModel(SoftDeleteModel):
name = models.CharField(max_length=255)
related = models.ForeignKey(RelatedModel, on_delete=models.CASCADE)
on_delete=models.CASCADE→ Related objects are also soft deleted.on_delete=models.SET_NULL→ The foreign key is set to NULL when soft deleting.on_delete=models.PROTECT→ Prevents soft deletion if related objects exist.
📜 Soft Delete Signals
You can hook into soft delete events using signals:
from soft_deletion.signals import pre_soft_delete, post_soft_delete
def pre_delete_handler(sender, instance, **kwargs):
print(f"About to soft delete: {instance}")
pre_soft_delete.connect(pre_delete_handler, sender=MyModel)
✅ Running Tests
To ensure everything works correctly, run:
pytest
Or using Django's test framework:
python manage.py test
Metadata
Release files for django-soft-deletion-model 1.0.0
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_soft_deletion_model-1.0.0.tar.gz | 7.1 kB | Without provenance | Details for django_soft_deletion_model-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_soft_deletion_model-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for django_soft_deletion_model-1.0.0-py3-none-any.whl |
Total release size: 16.1 kB
Release files / django_soft_deletion_model-1.0.0.tar.gz
| Download URL | django_soft_deletion_model-1.0.0.tar.gz |
|---|---|
| Size | 7.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
cab7dc0912f369b6dfb9c7f69638ca4ed1670f988c45f0b1cec1be56e0a9bc2e
|
|
BLAKE2b-256 checksum How to use checksums |
065b0162363799648ed64fdc2bae0fa7657ddbd1442d9be426fc87b1574f972d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.21
|
Release files / django_soft_deletion_model-1.0.0-py3-none-any.whl
| Download URL | django_soft_deletion_model-1.0.0-py3-none-any.whl |
|---|---|
| Size | 9.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4da828926ed8eccd76f87ae6e19fa1122f59246a4a20ebd1cf666cf3a677820f
|
|
BLAKE2b-256 checksum How to use checksums |
1d80358c0d86a2b23e8b748655c83ba6652ac54181d87ec6359509ec7b4a75c6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.21
|
Security for django-soft-deletion-model 1.0.0
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.