Detecting changes of model field values.
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
""" Detect changes of Django Models. | |
""" | |
class ChangeDetector: | |
def __init__(self, *args, **kwargs): | |
super().__init__(*args, **kwargs) | |
self._dirty_fields = {f: False for f in self._get_field_names()} | |
self._inited = True | |
def __setattr__(self, name, value): | |
super().__setattr__(name, value) | |
if hasattr(self, '_inited'): | |
if name in self._get_field_names(): | |
self._dirty_fields[name] = True | |
def _get_field_names(self): | |
return (f.name for f in self._meta.fields) | |
def is_dirty(self, fields=None): | |
fields = fields or self._get_field_names() | |
return any(self._dirty_fields[f] for f in fields) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment