Skip to content

Commit

Permalink
Use field.to_python to do django type conversions on the field before…
Browse files Browse the repository at this point in the history
… checking if dirty.


This solves issues where you might have a decimal field that you write a string to, eg:
>>> m = MyModel.objects.get(id=1)
>>> m.my_decimal_field
Decimal('1.00')
>>> m.my_decimal_field = u'1.00' # from a form or something
>>> m.is_dirty() # currently evaluates to True, should evaluate to False
False

This pull request could probably use some unit testing, but it should be safe as the base class for django fields defines to_python as:

def to_python(self, value):
  return value

So, any field type that does not have an explicit to_python method will behave as before this change.
  • Loading branch information
jimrollenhagen committed Mar 29, 2012
1 parent e2ac297 commit ba3282d
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/dirtyfields/dirtyfields.py
Expand Up @@ -9,7 +9,7 @@ def __init__(self, *args, **kwargs):
reset_state(sender=self.__class__, instance=self)

def _as_dict(self):
return dict([(f.name, getattr(self, f.name)) for f in self._meta.local_fields if not f.rel])
return dict([(f.name, f.to_python(getattr(self, f.name))) for f in self._meta.local_fields if not f.rel])

def get_dirty_fields(self):
new_state = self._as_dict()
Expand Down

0 comments on commit ba3282d

Please sign in to comment.