Skip to content

Commit

Permalink
Update M2M check mode to be disabled by default
Browse files Browse the repository at this point in the history
  • Loading branch information
romgar committed Aug 4, 2016
1 parent 7be20d5 commit fcc6492
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/dirtyfields/dirtyfields.py
Expand Up @@ -14,7 +14,7 @@ class DirtyFieldsMixin(object):

# This mode has been introduced to handle some situations like this one:
# https://github.com/romgar/django-dirtyfields/issues/73
ENABLE_M2M_CHECK = True
ENABLE_M2M_CHECK = False

def __init__(self, *args, **kwargs):
super(DirtyFieldsMixin, self).__init__(*args, **kwargs)
Expand Down
5 changes: 5 additions & 0 deletions tests/models.py
Expand Up @@ -68,6 +68,11 @@ class TestCurrentDatetimeModel(DirtyFieldsMixin, models.Model):

class TestM2MModel(DirtyFieldsMixin, models.Model):
m2m_field = models.ManyToManyField(TestModel)
ENABLE_M2M_CHECK = True


class TestM2MModelWithoutM2MModeEnabled(DirtyFieldsMixin, models.Model):
m2m_field = models.ManyToManyField(TestModel)


class TestModelWithCustomPK(DirtyFieldsMixin, models.Model):
Expand Down
12 changes: 11 additions & 1 deletion tests/test_m2m_fields.py
@@ -1,7 +1,7 @@
import pytest

from .models import TestModel, TestM2MModel, TestModelWithCustomPK, TestM2MModelWithCustomPKOnM2M, \
TestModelWithoutM2MCheck
TestModelWithoutM2MCheck, TestM2MModelWithoutM2MModeEnabled


@pytest.mark.django_db
Expand All @@ -23,6 +23,16 @@ def test_dirty_fields_on_m2m():
assert tm.get_dirty_fields(check_m2m={'m2m_field': set([0, tm2.id])}) == {'m2m_field': set([tm2.id])}


@pytest.mark.django_db
def test_dirty_fields_on_m2m_not_possible_if_not_enabled():
tm = TestM2MModelWithoutM2MModeEnabled.objects.create()
tm2 = TestModel.objects.create()
tm.m2m_field.add(tm2)

with pytest.raises(Exception):
assert tm.get_dirty_fields(check_m2m={'m2m_field': set([tm2.id])}) == {}


@pytest.mark.django_db
def test_m2m_check_with_custom_primary_key():
# test for bug: https://github.com/romgar/django-dirtyfields/issues/74
Expand Down

0 comments on commit fcc6492

Please sign in to comment.