Skip to content

Commit

Permalink
Merge branch 'release-0.6.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
romgar committed Jun 14, 2015
2 parents bcc0086 + 0713e33 commit e00e8af
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 2 deletions.
9 changes: 9 additions & 0 deletions ChangeLog.rst
Expand Up @@ -2,6 +2,15 @@ ChangeLog
=========


.. _v0.6.1:

0.6.1 (2015-06-14)
------------------

*Bugfix:*

- Preventing django db expressions to be evaluated when testing dirty fields (#39).


.. _v0.6:

Expand Down
1 change: 1 addition & 0 deletions README.rst
@@ -1,3 +1,4 @@

Django Dirty Fields
===================

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -5,7 +5,7 @@ def listify(filename):

setup(
name = "django-dirtyfields",
version = "0.6",
version = "0.6.1",
url = 'http://github.com/smn/django-dirtyfields',
license = 'BSD',
description = "Tracking dirty fields on a Django model instance",
Expand Down
10 changes: 10 additions & 0 deletions src/dirtyfields/compat.py
@@ -0,0 +1,10 @@

def is_db_expression(value):
try:
# django < 1.8
from django.db.models.expressions import ExpressionNode
return isinstance(value, ExpressionNode)
except ImportError:
# django >= 1.8 (big refactoring in Lookup/Expressions/Transforms)
from django.db.models.expressions import BaseExpression, Combinable
return isinstance(value, (BaseExpression, Combinable))
6 changes: 6 additions & 0 deletions src/dirtyfields/dirtyfields.py
@@ -1,6 +1,7 @@
# Adapted from http://stackoverflow.com/questions/110803/dirty-fields-in-django

from django.db.models.signals import post_save
from .compat import is_db_expression


class DirtyFieldsMixin(object):
Expand All @@ -21,6 +22,11 @@ def _as_dict(self, check_relationship):
continue

field_value = getattr(self, field.attname)

# If current field value is an expression, we are not evaluating it
if is_db_expression(field_value):
continue

all_field[field.name] = field.to_python(field_value)

return all_field
Expand Down
4 changes: 4 additions & 0 deletions tests/models.py
Expand Up @@ -42,3 +42,7 @@ class OrdinaryTestModelWithForeignKey(models.Model):

class SubclassModel(TestModel):
pass


class TestExpressionModel(DirtyFieldsMixin, models.Model):
counter = models.IntegerField(default=0)
13 changes: 12 additions & 1 deletion tests/tests.py
Expand Up @@ -6,7 +6,7 @@
from django.test.utils import override_settings
from .models import (TestModel, TestModelWithForeignKey, TestModelWithNonEditableFields, TestModelWithOneToOneField,
OrdinaryTestModel, OrdinaryTestModelWithForeignKey, TestModelWithSelfForeignKey,
SubclassModel, TestModelWithDecimalField)
SubclassModel, TestModelWithDecimalField, TestExpressionModel)


class DirtyFieldsMixinTestCase(TestCase):
Expand Down Expand Up @@ -212,3 +212,14 @@ def test_decimal_field_correctly_managed(self):

tm.decimal_field = u"2.00"
self.assertFalse(tm.is_dirty())

def test_expressions_not_taken_into_account_for_dirty_check(self):
# Non regression test case for bug:
# https://github.com/smn/django-dirtyfields/issues/39
from django.db.models import F
tm = TestExpressionModel.objects.create()
tm.counter = F('counter') + 1

# This save() was raising a ValidationError: [u"'F(counter) + Value(1)' value must be an integer."]
# caused by a call to_python() on an expression node
tm.save()

0 comments on commit e00e8af

Please sign in to comment.