|
12 | 12 | class DirtyFieldsMixin(object):
|
13 | 13 | compare_function = (raw_compare, {})
|
14 | 14 |
|
| 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 | + |
15 | 19 | def __init__(self, *args, **kwargs):
|
16 | 20 | super(DirtyFieldsMixin, self).__init__(*args, **kwargs)
|
17 | 21 | post_save.connect(
|
18 | 22 | reset_state, sender=self.__class__,
|
19 | 23 | dispatch_uid='{name}-DirtyFieldsMixin-sweeper'.format(
|
20 | 24 | name=self.__class__.__name__))
|
21 |
| - self._connect_m2m_relations() |
| 25 | + if self.enable_m2m_check: |
| 26 | + self._connect_m2m_relations() |
22 | 27 | reset_state(sender=self.__class__, instance=self)
|
23 | 28 |
|
24 | 29 | def _connect_m2m_relations(self):
|
@@ -83,6 +88,9 @@ def get_dirty_fields(self, check_relationship=False, check_m2m=None, verbose=Fal
|
83 | 88 | initial_dict = self._as_dict(check_relationship, include_primary_key=False)
|
84 | 89 | return initial_dict
|
85 | 90 |
|
| 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 | + |
86 | 94 | modified_fields = compare_states(self._as_dict(check_relationship),
|
87 | 95 | self._original_state,
|
88 | 96 | self.compare_function)
|
@@ -112,4 +120,5 @@ def reset_state(sender, instance, **kwargs):
|
112 | 120 | # original state should hold all possible dirty fields to avoid
|
113 | 121 | # getting a `KeyError` when checking if a field is dirty or not
|
114 | 122 | 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