Skip to content

Instantly share code, notes, and snippets.

Created December 20, 2012 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/4344064 to your computer and use it in GitHub Desktop.
Save anonymous/4344064 to your computer and use it in GitHub Desktop.
track Django models values changes.
from django.db import models
from django.db.models.signals import pre_save, post_init
def track_model_changes(sender, instance, **kwargs):
"""Save former data to compare with new data and track changed values"""
instance.__former = dict((field.name, field.value_from_object(instance)) for field in Report._meta.fields)
class Example(models.Model):
status = models.IntegerField()
post_init.connect(track_model_changes, sender=Example)
@receiver(pre_save, sender=Example)
def check_status(sender, instance, **kwargs):
if report.__former['status'] != report.status:
print "status changed"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment