[Solved]-Can you find out if a Django Model instance is "dirty"?

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

1๐Ÿ‘

try use lck.django class TimeTrackable

๐Ÿ‘คw.matyskiewicz

Leave a comment