Kaarsemaker.net


changes.py
773 bytes

15 lines

Download

auth_ldap.py
filtercache.py

Preview

 1 # Dedicated to the public domain
 2 # Add this snippet somewhere in your models.py (or import this as separate
 3 # file), all objects will then have a changes method, listing all fields that
 4 # have changed in a dict of the form {fieldname: (old_value, new_value)}. If
 5 # the object is new, the old_value of all fields will be None
 6 
 7 from django.db import models
 8 
 9 def changes(self):
10     fields = [x.name for x in self._meta._fields() if not (x.name.endswith('_ptr_id') or x.name.endswith('_id'))]
11     if not self.id:
12         return dict([(x, (None, getattr(self, x))) for x in fields])
13     old = self.__class__.objects.get(id=self.id)
14     return dict([(x, (getattr(old, x), getattr(self, x))) for x in fields if getattr(self, x) != getattr(old, x)])
15 models.Model.changes = changes
 1 # Dedicated to the public domain
 2 # Add this snippet somewhere in your models.py (or import this as separate
 3 # file), all objects will then have a changes method, listing all fields that
 4 # have changed in a dict of the form {fieldname: (old_value, new_value)}. If
 5 # the object is new, the old_value of all fields will be None
 6 
 7 from django.db import models
 8 
 9 def changes(self):
10     fields = [x.name for x in self._meta._fields() if not (x.name.endswith('_ptr_id') or x.name.endswith('_id'))]
11     if not self.id:
12         return dict([(x, (None, getattr(self, x))) for x in fields])
13     old = self.__class__.objects.get(id=self.id)
14     return dict([(x, (getattr(old, x), getattr(self, x))) for x in fields if getattr(self, x) != getattr(old, x)])
15 models.Model.changes = changes

Show all