diff --git a/src/dirtyfields/dirtyfields.py b/src/dirtyfields/dirtyfields.py index ece023a..1c11f01 100644 --- a/src/dirtyfields/dirtyfields.py +++ b/src/dirtyfields/dirtyfields.py @@ -17,6 +17,11 @@ class DirtyFieldsMixin(object): # https://github.com/romgar/django-dirtyfields/issues/73 ENABLE_M2M_CHECK = False + # Flag to track M2M changes as a simple boolean; less accurate than + # ENABLE_M2M_CHECK as we're only tracking if m2m relations were updated, + # not whether they are dirty + ENABLE_BASIC_M2M_CHECK = False + FIELDS_TO_CHECK = None def __init__(self, *args, **kwargs): @@ -25,14 +30,15 @@ def __init__(self, *args, **kwargs): reset_state, sender=self.__class__, weak=False, dispatch_uid='{name}-DirtyFieldsMixin-sweeper'.format( name=self.__class__.__name__)) - if self.ENABLE_M2M_CHECK: + if self.ENABLE_M2M_CHECK or self.ENABLE_BASIC_M2M_CHECK: self._connect_m2m_relations() reset_state(sender=self.__class__, instance=self) def _connect_m2m_relations(self): + m2m_handler = set_m2m_dirty if self.ENABLE_BASIC_M2M_CHECK else reset_state for m2m_field, model in get_m2m_with_model(self.__class__): m2m_changed.connect( - reset_state, sender=remote_field(m2m_field).through, weak=False, + m2m_handler, sender=remote_field(m2m_field).through, weak=False, dispatch_uid='{name}-DirtyFieldsMixin-sweeper-m2m'.format( name=self.__class__.__name__)) @@ -121,6 +127,8 @@ def get_dirty_fields(self, check_relationship=False, check_m2m=None, verbose=Fal return modified_fields def is_dirty(self, check_relationship=False, check_m2m=None): + if self.ENABLE_BASIC_M2M_CHECK and self._m2m_dirty: + return True return {} != self.get_dirty_fields(check_relationship=check_relationship, check_m2m=check_m2m) @@ -164,3 +172,9 @@ def reset_state(sender, instance, **kwargs): instance._original_state = new_state if instance.ENABLE_M2M_CHECK: instance._original_m2m_state = instance._as_dict_m2m() + elif instance.ENABLE_BASIC_M2M_CHECK: + instance._m2m_dirty = False + + +def set_m2m_dirty(sender, instance, **kwargs): + instance._m2m_dirty = True