# Dedicated to the public domain
# Add this snippet somewhere in your models.py (or import this as separate
# file), all objects will then have a changes method, listing all fields that
# have changed in a dict of the form {fieldname: (old_value, new_value)}. If
# the object is new, the old_value of all fields will be None

from django.db import models

def changes(self):
    fields = [x.name for x in self._meta._fields() if not (x.name.endswith('_ptr_id') or x.name.endswith('_id'))]
    if not self.id:
        return dict([(x, (None, getattr(self, x))) for x in fields])
    old = self.__class__.objects.get(id=self.id)
    return dict([(x, (getattr(old, x), getattr(self, x))) for x in fields if getattr(self, x) != getattr(old, x)])
models.Model.changes = changes

