Skip to content

Commit e222717

Browse files
committed
Add an option to deactivate m2m checks, if not needed
1 parent cdf1ef0 commit e222717

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/dirtyfields/dirtyfields.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,18 @@
1212
class DirtyFieldsMixin(object):
1313
compare_function = (raw_compare, {})
1414

15+
# This mode has been introduced to handle some situations like this one:
16+
# https://github.com/romgar/django-dirtyfields/issues/73
17+
enable_m2m_check = True
18+
1519
def __init__(self, *args, **kwargs):
1620
super(DirtyFieldsMixin, self).__init__(*args, **kwargs)
1721
post_save.connect(
1822
reset_state, sender=self.__class__,
1923
dispatch_uid='{name}-DirtyFieldsMixin-sweeper'.format(
2024
name=self.__class__.__name__))
21-
self._connect_m2m_relations()
25+
if self.enable_m2m_check:
26+
self._connect_m2m_relations()
2227
reset_state(sender=self.__class__, instance=self)
2328

2429
def _connect_m2m_relations(self):
@@ -83,6 +88,9 @@ def get_dirty_fields(self, check_relationship=False, check_m2m=None, verbose=Fal
8388
initial_dict = self._as_dict(check_relationship, include_primary_key=False)
8489
return initial_dict
8590

91+
if check_m2m is not None and not self.enable_m2m_check:
92+
raise Exception("You can't check m2m fields if enable_m2m_check is set to False")
93+
8694
modified_fields = compare_states(self._as_dict(check_relationship),
8795
self._original_state,
8896
self.compare_function)
@@ -112,4 +120,5 @@ def reset_state(sender, instance, **kwargs):
112120
# original state should hold all possible dirty fields to avoid
113121
# getting a `KeyError` when checking if a field is dirty or not
114122
instance._original_state = instance._as_dict(check_relationship=True)
115-
instance._original_m2m_state = instance._as_dict_m2m()
123+
if instance.enable_m2m_check:
124+
instance._original_m2m_state = instance._as_dict_m2m()

0 commit comments

Comments
 (0)