Skip to content

Commit f98fec8

Browse files
committed
fix(diff_against): support diffing against deleted records
1 parent ac44d22 commit f98fec8

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Authors
4343
- Dmytro Shyshov (`xahgmah <https://github.com/xahgmah>`_)
4444
- Edouard Richard (`vied12 <https://github.com/vied12>` _)
4545
- Eduardo Cuducos
46+
- Eric Uriostigue (`euriostigue <https://github.com/euriostigue>`_)
4647
- Erik van Widenfelt (`erikvw <https://github.com/erikvw>`_)
4748
- Fábio Capuano (`fabiocapsouza <https://github.com/fabiocapsouza`_)
4849
- Filipe Pina (@fopina)

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Changes
33

44
Unreleased
55
----------
6+
- Fixed `diff_against` to work with deleted objects (gh-1312)
67

78

89
3.5.0 (2024-02-19)

simple_history/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -952,8 +952,8 @@ def diff_against(self, old_history, excluded_fields=None, included_fields=None):
952952
current_values = model_to_dict(self, fields=fields)
953953

954954
for field in fields:
955-
old_value = old_values[field]
956-
current_value = current_values[field]
955+
old_value = None if old_history.history_type == "-" else old_values[field]
956+
current_value = None if self.history_type == "-" else current_values[field]
957957

958958
if old_value != current_value:
959959
changes.append(ModelChange(field, old_value, current_value))

simple_history/tests/tests/test_models.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import warnings
44
from datetime import datetime, timedelta
55

6-
import django
76
from django.apps import apps
87
from django.conf import settings
98
from django.contrib.auth import get_user_model
@@ -28,7 +27,6 @@
2827
pre_create_historical_m2m_records,
2928
pre_create_historical_record,
3029
)
31-
from simple_history.tests.custom_user.models import CustomUser
3230
from simple_history.tests.tests.utils import (
3331
database_router_override_settings,
3432
database_router_override_settings_history_in_diff_db,
@@ -72,7 +70,6 @@
7270
HistoricalCustomFKError,
7371
HistoricalPoll,
7472
HistoricalPollWithHistoricalIPAddress,
75-
HistoricalPollWithManyToMany_places,
7673
HistoricalState,
7774
InheritedRestaurant,
7875
Library,
@@ -85,8 +82,6 @@
8582
ModelWithMultipleNoDBIndex,
8683
ModelWithSingleNoDBIndexUnique,
8784
MultiOneToOne,
88-
MyOverrideModelNameRegisterMethod1,
89-
OverrideModelNameAsCallable,
9085
OverrideModelNameUsingBaseModel1,
9186
Person,
9287
Place,
@@ -800,6 +795,33 @@ def test_history_with_unknown_field(self):
800795
with self.assertNumQueries(0):
801796
new_record.diff_against(old_record, excluded_fields=["unknown_field"])
802797

798+
def test_history_with_deletion_record(self):
799+
question = "what's up?"
800+
p = Poll.objects.create(question=question, pub_date=today)
801+
poll_pk = p.pk
802+
new_record = p.history.first()
803+
p.delete()
804+
805+
deletion_record = HistoricalPoll.objects.get(id=poll_pk, history_type="-")
806+
807+
with self.assertNumQueries(0):
808+
delta = new_record.diff_against(
809+
deletion_record, included_fields=["question"]
810+
)
811+
self.assertEqual(delta.changed_fields, ["question"])
812+
self.assertEqual(len(delta.changes), 1)
813+
self.assertEqual(delta.changes[0].new, question)
814+
self.assertEqual(delta.changes[0].old, None)
815+
816+
with self.assertNumQueries(0):
817+
delta = deletion_record.diff_against(
818+
new_record, included_fields=["question"]
819+
)
820+
self.assertEqual(delta.changed_fields, ["question"])
821+
self.assertEqual(len(delta.changes), 1)
822+
self.assertEqual(delta.changes[0].new, None)
823+
self.assertEqual(delta.changes[0].old, question)
824+
803825

804826
class GetPrevRecordAndNextRecordTestCase(TestCase):
805827
def assertRecordsMatch(self, record_a, record_b):

0 commit comments

Comments
 (0)