Skip to content

Commit 2ffc483

Browse files
RealOrangeOnejeking3
authored andcommitted
Simply check the history attributes rather than serializing.
This stops unnecesary queries from `model_to_dict` for M2M values
1 parent 650213f commit 2ffc483

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

simple_history/models.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from django.db import models
1212
from django.db.models import ManyToManyField
1313
from django.db.models.fields.proxy import OrderWrt
14-
from django.forms.models import model_to_dict
1514
from django.urls import reverse
1615
from django.utils import timezone
1716
from django.utils.encoding import smart_str
@@ -594,20 +593,26 @@ def diff_against(self, old_history, excluded_fields=None):
594593
)
595594
)
596595
if excluded_fields is None:
597-
excluded_fields = []
596+
excluded_fields = set()
597+
598+
excluded_fields = set(excluded_fields).union(self._history_excluded_fields)
599+
600+
fields = {
601+
f.name
602+
for f in old_history.instance_type._meta.fields
603+
if f.name not in excluded_fields
604+
}
605+
598606
changes = []
599607
changed_fields = []
600-
old_values = model_to_dict(old_history.instance)
601-
current_values = model_to_dict(self.instance)
602-
for field, new_value in current_values.items():
603-
if field in excluded_fields:
604-
continue
605-
if field in old_values:
606-
old_value = old_values[field]
607-
if old_value != new_value:
608-
change = ModelChange(field, old_value, new_value)
609-
changes.append(change)
610-
changed_fields.append(field)
608+
609+
for field in fields:
610+
old_value = getattr(old_history, field)
611+
current_value = getattr(self, field)
612+
613+
if old_value != current_value:
614+
changes.append(ModelChange(field, old_value, current_value))
615+
changed_fields.append(field)
611616

612617
return ModelDelta(changes, changed_fields, old_history, self)
613618

0 commit comments

Comments
 (0)