python
42 lines · 10 steps
How soft deletes work in Django
A reusable abstract model that marks rows deleted with a timestamp instead of removing them.
Explained by
highlit
1from django.db import models
2from django.utils import timezone
3
4
5class SoftDeleteQuerySet(models.QuerySet):
6 def delete(self):
7 return super().update(deleted_at=timezone.now())
8
9 def hard_delete(self):
10 return super().delete()
11
12 def alive(self):
13 return self.filter(deleted_at__isnull=True)
14
15 def dead(self):
16 return self.filter(deleted_at__isnull=False)
17
18
19class SoftDeleteManager(models.Manager):
20 def get_queryset(self):
21 return SoftDeleteQuerySet(self.model, using=self._db).alive()
22
23
24class SoftDeleteModel(models.Model):
25 deleted_at = models.DateTimeField(null=True, blank=True, editable=False)
26
27 objects = SoftDeleteManager()
28 all_objects = SoftDeleteQuerySet.as_manager()
29
30 class Meta:
31 abstract = True
32
33 def delete(self, using=None, keep_parents=False):
34 self.deleted_at = timezone.now()
35 self.save(using=using, update_fields=["deleted_at"])
36
37 def hard_delete(self, using=None, keep_parents=False):
38 super().delete(using=using, keep_parents=keep_parents)
39
40 def restore(self):
41 self.deleted_at = None
42 self.save(update_fields=["deleted_at"])
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Overriding delete to set a timestamp keeps rows recoverable while hiding them from normal queries.
- 2A custom manager that filters in get_queryset makes soft-deleted rows invisible by default everywhere.
- 3Pairing a default manager with an unfiltered one lets you reach hidden rows when you genuinely need them.
Related explainers
python
from fastapi import FastAPI, WebSocket, WebSocketDisconnect app = FastAPI()
Building a WebSocket chat with FastAPI
websockets
broadcast
connection-management
Intermediate
9 steps
python
import time import uuid from django.utils.deprecation import MiddlewareMixin
Attaching per-request context in Django
middleware
request lifecycle
multi-tenancy
Intermediate
7 steps
python
import random from typing import Iterator, List
How reservoir sampling picks k items
reservoir-sampling
streaming
randomness
Intermediate
5 steps
python
import secrets from django.contrib.auth import authenticate, login from django.core.cache import cache
Two-factor login with OTP in Django
two-factor-auth
one-time-passwords
caching
Intermediate
9 steps
python
import re from functools import total_ordering from typing import Optional
Parsing and comparing semantic versions
regex
operator-overloading
sorting
Intermediate
7 steps
python
from typing import Any, Sequence, Mapping def render_markdown_table(
Rendering an aligned Markdown table in Python
string formatting
data transformation
closures
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/how-soft-deletes-work-in-django-explained-python-bdb6/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.