django-softdelete
raw JSON → 0.11.5 verified Fri May 01 auth: no python
Soft delete support for Django ORM, adding a 'deleted' field and custom manager to objects instead of hard-deleting them. Current version: 0.11.5. Release cadence is intermittent, with no recent updates.
pip install django-softdelete Common errors
error ImportError: cannot import name 'SoftDeleteObject' from 'softdelete' ↓
cause Incorrect import path; SoftDeleteObject is in softdelete.models, not softdelete directly.
fix
Use: from softdelete.models import SoftDeleteObject
error django.core.exceptions.FieldDoesNotExist: MyModel has no field named 'deleted_at' ↓
cause The model does not inherit from SoftDeleteObject or does not have the field.
fix
Ensure your model inherits from SoftDeleteObject and run makemigrations.
Warnings
gotcha By default, all model instances with a non-null deleted_at are excluded from queries using the default manager. Use objects_all (or the all_objects manager) to include soft-deleted instances. ↓
fix Use MyModel.all_objects.filter() or MyModel.objects_all to query all records.
deprecated The library uses a 'deleted_at' field. Ensure your database supports nullable datetime fields. ↓
fix No fix needed; just be aware of the field type.
breaking The library does not support Django's built-in soft delete from Django 4.x or later. It is a separate implementation. ↓
fix Do not mix with Django's official soft delete features.
Imports
- SoftDeleteManager
from softdelete.models import SoftDeleteManager - SoftDeleteObject
from softdelete.models import SoftDeleteObject
Quickstart
from django.db import models
from softdelete.models import SoftDeleteObject, SoftDeleteManager
class MyModel(SoftDeleteObject):
name = models.CharField(max_length=100)
objects = SoftDeleteManager()
# Soft delete
obj = MyModel.objects.create(name='test')
obj.delete() # Sets deleted_at timestamp, does not remove from DB
# Restore
obj.undelete()
# Hard delete (bypass soft delete)
obj.hard_delete()