[Fixed]-Django – Check diference between old and new value when overriding save method

20👍

You can store the old value inside the init method:

def __init__(self, *args, **kwargs):
    super(MyModel, self).__init__(*args, **kwargs)
    self.old_my_field = self.my_field

def save(self, *args, **kwargs):
    print self.old_my_field
    print self.my_field

You can probably use deepcopy or something alike to copy the whole object for later use in the save and delete methods.

11👍

Django doesn’t cache the old values of the model instance, so you need to do that yourself or perform another query before save.

One common pattern is to use a pre-save signal (or put this code directly in your save() method, as you’ve done):

old_instance = MyModel.objects.get(pk=instance.pk)
# compare instance with old_instance, and maybe decide whether to continue

If you want to keep a cache of the old values, then you would probably do that in your view code:

from copy import deepcopy
object = MyModel.objects.get(pk=some_value)
cache = deepcopy(object)

# Do something with object, and then compare with cache before saving

There was a recent discussion on django-developers about this as well, with some other possible solutions.

1👍

I am checking the difference to old values using a django-reversion signal, but the same logic would apply to the save signals. The difference for me being that I want to save whether the field was saved or not.

@receiver(reversion.pre_revision_commit)
def it_worked(sender, **kwargs):
    currentVersion = kwargs.pop('versions')[0].field_dict
    fieldList = currentVersion.keys()
    fieldList.remove('id')
    commentDict = {}
    print fieldList
    try:
        pastVersion = reversion.get_for_object(kwargs.pop('instances')[0])[0].field_dict
    except IndexError:
        for field in fieldList:
            commentDict[field] = "Created"
        comment = commentDict
    except TypeError:
        for field in fieldList:
            commentDict[field] = "Deleted"
        comment = commentDict
    else:
        for field in fieldList:
            try:
                pastTest = pastVersion[field]
            except KeyError:
                commentDict[field] = "Created"
            else:       
                if currentVersion[field] != pastTest:
                    commentDict[field] = "Changed"
                else:
                    commentDict[field] = "Unchanged"
        comment = commentDict
    revision = kwargs.pop('revision')
    revision.comment = comment
    revision.save()
    kwargs['revision'] = revision
    sender.save_revision

Leave a comment