From 9d252f3310a8fe507188bb065d7e785a51d0a587 Mon Sep 17 00:00:00 2001 From: Jan-Jelle Kester Date: Wed, 3 Jun 2015 15:50:41 +0200 Subject: [PATCH] Respect default values and non-string objects in diffs --- src/auditlog/diff.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/auditlog/diff.py b/src/auditlog/diff.py index b8194e2..2db3c3c 100644 --- a/src/auditlog/diff.py +++ b/src/auditlog/diff.py @@ -1,15 +1,15 @@ from __future__ import unicode_literals from django.core.exceptions import ObjectDoesNotExist -from django.db.models import Model +from django.db.models import Model, NOT_PROVIDED from django.utils.encoding import smart_text def model_instance_diff(old, new, **kwargs): """ Calculate the differences between two model instances. One of the instances may be None (i.e., a newly - created model or deleted model). This will cause all fields with a value to have changed (from None). - + created model or deleted model). This will cause all fields with a value to have changed (from the fields default + value). """ from auditlog.registry import auditlog @@ -48,17 +48,17 @@ def model_instance_diff(old, new, **kwargs): for field in fields: try: - old_value = smart_text(getattr(old, field.name, None)) + old_value = getattr(old, field.name, None) except ObjectDoesNotExist: - old_value = None + old_value = field.default if field.default is not NOT_PROVIDED else None try: - new_value = smart_text(getattr(new, field.name, None)) + new_value = getattr(new, field.name, None) except ObjectDoesNotExist: new_value = None if old_value != new_value: - diff[field.name] = (old_value, new_value) + diff[field.name] = (smart_text(old_value), smart_text(new_value)) if len(diff) == 0: diff = None