Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Detecting changes of model field values.
""" 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 ( 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