Skip to content

Commit 4fd3ac7

Browse files
committed
Fix m2m diff_against included and excluded fields
1 parent 693a591 commit 4fd3ac7

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

simple_history/models.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -923,12 +923,20 @@ def diff_against(self, old_history, excluded_fields=None, included_fields=None):
923923
if excluded_fields is None:
924924
excluded_fields = set()
925925

926+
included_m2m_fields = {field.name for field in old_history._history_m2m_fields}
926927
if included_fields is None:
927928
included_fields = {
928929
f.name for f in old_history.instance_type._meta.fields if f.editable
929930
}
931+
else:
932+
included_m2m_fields = included_m2m_fields.intersection(included_fields)
930933

931-
fields = set(included_fields).difference(excluded_fields)
934+
fields = (
935+
set(included_fields)
936+
.difference(included_m2m_fields)
937+
.difference(excluded_fields)
938+
)
939+
m2m_fields = set(included_m2m_fields).difference(excluded_fields)
932940

933941
changes = []
934942
changed_fields = []
@@ -945,11 +953,10 @@ def diff_against(self, old_history, excluded_fields=None, included_fields=None):
945953
changed_fields.append(field)
946954

947955
# Separately compare m2m fields:
948-
for field in old_history._history_m2m_fields:
956+
for field in m2m_fields:
949957
# First retrieve a single item to get the field names from:
950958
reference_history_m2m_item = (
951-
getattr(old_history, field.name).first()
952-
or getattr(self, field.name).first()
959+
getattr(old_history, field).first() or getattr(self, field).first()
953960
)
954961
history_field_names = []
955962
if reference_history_m2m_item:
@@ -963,15 +970,13 @@ def diff_against(self, old_history, excluded_fields=None, included_fields=None):
963970
if f.editable and f.name not in ["id", "m2m_history_id", "history"]
964971
]
965972

966-
old_rows = list(
967-
getattr(old_history, field.name).values(*history_field_names)
968-
)
969-
new_rows = list(getattr(self, field.name).values(*history_field_names))
973+
old_rows = list(getattr(old_history, field).values(*history_field_names))
974+
new_rows = list(getattr(self, field).values(*history_field_names))
970975

971976
if old_rows != new_rows:
972-
change = ModelChange(field.name, old_rows, new_rows)
977+
change = ModelChange(field, old_rows, new_rows)
973978
changes.append(change)
974-
changed_fields.append(field.name)
979+
changed_fields.append(field)
975980

976981
return ModelDelta(changes, changed_fields, old_history, self)
977982

simple_history/tests/tests/test_models.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2126,6 +2126,17 @@ def test_diff_against(self):
21262126
self.assertListEqual(expected_change.new, delta.changes[0].new)
21272127
self.assertListEqual(expected_change.old, delta.changes[0].old)
21282128

2129+
delta = add_record.diff_against(create_record, included_fields=["places"])
2130+
self.assertEqual(delta.changed_fields, ["places"])
2131+
self.assertEqual(delta.old_record, create_record)
2132+
self.assertEqual(delta.new_record, add_record)
2133+
self.assertEqual(expected_change.field, delta.changes[0].field)
2134+
2135+
delta = add_record.diff_against(create_record, excluded_fields=["places"])
2136+
self.assertEqual(delta.changed_fields, [])
2137+
self.assertEqual(delta.old_record, create_record)
2138+
self.assertEqual(delta.new_record, add_record)
2139+
21292140
self.poll.places.clear()
21302141

21312142
# First and third records are effectively the same.

0 commit comments

Comments
 (0)