Last active
August 29, 2015 14:09
-
-
Save pablasso/cdf7add81f90384285ce to your computer and use it in GitHub Desktop.
This is from this thread: http://stackoverflow.com/a/13842223/19329 patched with the suggested fixes to avoid infinite loops while using objects.raw() or objects.only()
This file contains 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.forms.models import model_to_dict | |
class ModelDiffMixin(object): | |
""" | |
A model mixin that tracks model fields' values and provide some useful api | |
to know what fields have been changed. | |
""" | |
def __init__(self, *args, **kwargs): | |
super(ModelDiffMixin, self).__init__(*args, **kwargs) | |
self.__initial = self._dict | |
@property | |
def diff(self): | |
d1 = self.__initial | |
d2 = self._dict | |
diffs = [(k, (v, d2[k])) for k, v in d1.items() if v != d2[k]] | |
return dict(diffs) | |
@property | |
def has_changed(self): | |
return bool(self.diff) | |
@property | |
def changed_fields(self): | |
return self.diff.keys() | |
def get_field_diff(self, field_name): | |
""" | |
Returns a diff for field if it's changed and None otherwise. | |
""" | |
return self.diff.get(field_name, None) | |
def save(self, *args, **kwargs): | |
""" | |
Saves model and set initial state. | |
""" | |
super(ModelDiffMixin, self).save(*args, **kwargs) | |
self.__initial = self._dict | |
@property | |
def _dict(self): | |
from django.db.models.query_utils import DeferredAttribute | |
exclude = [f.name for f in self._meta.fields if isinstance(self.__class__.__dict__.get(f.attname), DeferredAttribute)] | |
return model_to_dict(self, fields=[field.name for field in self._meta.fields], exclude=exclude) |
This file contains 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
>>> p = Place() | |
>>> p.has_changed | |
False | |
>>> p.changed_fields | |
[] | |
>>> p.rank = 42 | |
>>> p.has_changed | |
True | |
>>> p.changed_fields | |
['rank'] | |
>>> p.diff | |
{'rank': (0, 42)} | |
>>> p.categories = [1, 3, 5] | |
>>> p.diff | |
{'categories': (None, [1, 3, 5]), 'rank': (0, 42)} | |
>>> p.get_field_diff('categories') | |
(None, [1, 3, 5]) | |
>>> p.get_field_diff('rank') | |
(0, 42) | |
>>> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment