Skip to content

Commit 2ee12d9

Browse files
rwlogelThePumpingLemma
authored andcommitted
Add feature to allow historical records to be deleted when master is deleted
1 parent 3726327 commit 2ee12d9

File tree

5 files changed

+41
-4
lines changed

5 files changed

+41
-4
lines changed

CHANGES.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Changes
22
=======
33

4+
Unreleased
5+
----------
6+
- Add ability to cascade delete historical records when master record is deleted
7+
48
2.4.0 (2018-09-20)
59
------------------
610
- Add pre and post create_historical_record signals (gh-426)

docs/advanced.rst

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,19 @@ If you want to save a model without a historical record, you can use the followi
424424
poll = Poll(question='something')
425425
poll.save_without_historical_record()
426426
427+
Deleting historical record
428+
--------------------------
429+
430+
In some circumstances you may want to delete all the historical records when the master record is deleted. This can
431+
be accomplished by setting ``cascade_delete_history=True``.
432+
433+
.. code-block:: python
434+
435+
class Poll(models.Model):
436+
question = models.CharField(max_length=200)
437+
history = HistoricalRecords(cascade_delete_history=True)
438+
439+
427440
History Diffing
428441
-------------------
429442

@@ -465,4 +478,4 @@ django-simple-history includes signals that helps you provide custom behaviour w
465478
466479
@receiver(pre_create_historical_record)
467480
def post_create_historical_record(sender, instance, history_instance, **kwargs):
468-
print("Sent after saving historical record")
481+
print("Sent after saving historical record")

simple_history/models.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ def __init__(self, verbose_name=None, bases=(models.Model,),
4444
user_related_name='+', table_name=None, inherit=False,
4545
excluded_fields=None, history_id_field=None,
4646
history_change_reason_field=None,
47-
user_model=None, get_user=default_get_user):
47+
user_model=None, get_user=default_get_user,
48+
cascade_delete_history=False):
4849
self.user_set_verbose_name = verbose_name
4950
self.user_related_name = user_related_name
5051
self.table_name = table_name
@@ -53,6 +54,7 @@ def __init__(self, verbose_name=None, bases=(models.Model,),
5354
self.history_change_reason_field = history_change_reason_field
5455
self.user_model = user_model
5556
self.get_user = get_user
57+
self.cascade_delete_history = cascade_delete_history
5658
if excluded_fields is None:
5759
excluded_fields = []
5860
self.excluded_fields = excluded_fields
@@ -340,7 +342,11 @@ def post_save(self, instance, created, **kwargs):
340342
self.create_historical_record(instance, created and '+' or '~')
341343

342344
def post_delete(self, instance, **kwargs):
343-
self.create_historical_record(instance, '-')
345+
if self.cascade_delete_history:
346+
manager = getattr(instance, self.manager_name)
347+
manager.all().delete()
348+
else:
349+
self.create_historical_record(instance, '-')
344350

345351
def create_historical_record(self, instance, history_type):
346352
history_date = getattr(instance, '_history_date', now())

simple_history/tests/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class WaterLevel(models.Model):
6161
level = models.IntegerField()
6262
date = models.DateTimeField()
6363

64-
history = HistoricalRecords()
64+
history = HistoricalRecords(cascade_delete_history=True)
6565

6666
@property
6767
def _history_date(self):

simple_history/tests/tests/test_models.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,20 @@ def test_delete_verify_change_reason_explicity(self):
170170
'history_type': "-"
171171
})
172172

173+
def test_cascade_delete_history(self):
174+
thames = WaterLevel.objects.create(waters="Thames", level=2.5,
175+
date=today)
176+
nile = WaterLevel.objects.create(waters="Nile", level=2.5,
177+
date=today)
178+
179+
self.assertEqual(len(thames.history.all()), 1)
180+
self.assertEqual(len(nile.history.all()), 1)
181+
182+
nile.delete()
183+
184+
self.assertEqual(len(thames.history.all()), 1)
185+
self.assertEqual(len(nile.history.all()), 0)
186+
173187
def test_save_without_historical_record(self):
174188
pizza_place = Restaurant.objects.create(name='Pizza Place', rating=3)
175189
pizza_place.rating = 4

0 commit comments

Comments
 (0)