instance.data_updated_on_presave = 'presave_value' pre_save.connect(TestModelWithPreSaveSignal.pre_save, sender=TestModelWithPreSaveSignal) class TestModelWithoutM2MCheck(DirtyFieldsMixin, models.Model): characters = models.CharField(blank=True, max_length=80) ENABLE_M2M_CHECK = False class TestDoubleForeignKeyModel(DirtyFieldsMixin, models.Model): fkey1 = models.ForeignKey(TestModel) fkey2 = models.ForeignKey(TestModel, null=True, related_name='fkey2') if is_postgresql_env_with_json_field(): from django.contrib.postgres.fields import JSONField class TestModelWithJSONField(DirtyFieldsMixin, models.Model): json_field = JSONField() class TestModelWithSpecifiedFields(DirtyFieldsMixin, models.Model): boolean1 = models.BooleanField(default=True) boolean2 = models.BooleanField(default=True) FIELDS_TO_CHECK = ['boolean1'] class TestModelWithM2MAndSpecifiedFields(DirtyFieldsMixin, models.Model): m2m1 = models.ManyToManyField(TestModel) m2m2 = models.ManyToManyField(TestModel)
class TestModelWithPreSaveSignal(DirtyFieldsMixin, models.Model): data = models.CharField(max_length=255) data_updated_on_presave = models.CharField(max_length=255, blank=True, null=True) @staticmethod def pre_save(instance, *args, **kwargs): dirty_fields = instance.get_dirty_fields() # only works for case2 if 'data' in dirty_fields: if 'specific_value' in instance.data: instance.data_updated_on_presave = 'presave_value' pre_save.connect(TestModelWithPreSaveSignal.pre_save, sender=TestModelWithPreSaveSignal) class TestModelWithoutM2MCheck(DirtyFieldsMixin, models.Model): characters = models.CharField(blank=True, max_length=80) ENABLE_M2M_CHECK = False class TestDoubleForeignKeyModel(DirtyFieldsMixin, models.Model): fkey1 = models.ForeignKey(TestModel) fkey2 = models.ForeignKey(TestModel, null=True, related_name='fkey2') if is_postgresql_env_with_json_field(): from django.contrib.postgres.fields import JSONField class TestModelWithJSONField(DirtyFieldsMixin, models.Model): json_field = JSONField()
import pytest from tests.utils import is_postgresql_env_with_json_field @pytest.mark.skipif(not is_postgresql_env_with_json_field(), reason="requires postgresql >= 9.0.4") @pytest.mark.django_db def test_dirty_json_field(): from tests.models import TestModelWithJSONField tm = TestModelWithJSONField.objects.create(json_field={'data': [1, 2, 3]}) data = tm.json_field['data'] data.append(4) assert tm.get_dirty_fields(verbose=True) == { 'json_field': { 'current': {'data': [1, 2, 3, 4]}, 'saved': {'data': [1, 2, 3]} } }
import pytest from tests.utils import is_postgresql_env_with_json_field @pytest.mark.skipif(not is_postgresql_env_with_json_field(), reason="requires postgresql and Django 1.9+") @pytest.mark.django_db def test_dirty_json_field(): from tests.models import TestModelWithJSONField tm = TestModelWithJSONField.objects.create(json_field={'data': [1, 2, 3]}) data = tm.json_field['data'] data.append(4) assert tm.get_dirty_fields(verbose=True) == { 'json_field': { 'current': {'data': [1, 2, 3, 4]}, 'saved': {'data': [1, 2, 3]} } }