Created
December 20, 2012 09:17
-
-
Save anonymous/4344064 to your computer and use it in GitHub Desktop.
track Django models values changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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