4๐
โ
A solution that does do a database query:
class DirtyMixin(object):
@property
def is_dirty(self):
db_obj = self.__class__.objects.get(self.pk)
for f in self._meta.local_fields:
if self.__getattribute__(f.name) != db_obj.__getattribute__(f.name):
return True
return False
You can then add this as an ancestor class to a model. Or, monkey-patch the forms.Model class, if you like.
from django.db import models
models.Model.__bases__ = (DirtyMixin,) + models.Model.__bases__
2๐
Another way, involving overriding __setattr__
, is discussed at some length in this Django ticket.
๐คVinay Sajip
Source:stackexchange.com