summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaude Paroz <claude@2xlibre.net>2015-12-16 23:07:17 +0100
committerClaude Paroz <claude@2xlibre.net>2015-12-17 15:38:19 +0100
commitd91cc25a2a43cb2526943dc2285ffe59e38fabfd (patch)
tree4a2c3a630ec900f8ed4210cf9c5fdf762e56c48b
parent2ec23a3d41be2ba5df9f46b17e0a05eb1b051c41 (diff)
Fixed #25942 -- Fixed TypedChoiceField.has_changed with nullable field
This fixes a regression introduced by 871440361.
-rw-r--r--django/forms/fields.py11
-rw-r--r--docs/releases/1.9.1.txt3
-rw-r--r--tests/forms_tests/tests/test_fields.py8
3 files changed, 16 insertions, 6 deletions
diff --git a/django/forms/fields.py b/django/forms/fields.py
index 55f225e65b..ac615bbe68 100644
--- a/django/forms/fields.py
+++ b/django/forms/fields.py
@@ -181,17 +181,16 @@ class Field(object):
"""
Return True if data differs from initial.
"""
- # For purposes of seeing whether something has changed, None is
- # the same as an empty string, if the data or initial value we get
- # is None, replace it w/ ''.
- initial_value = initial if initial is not None else ''
try:
data = self.to_python(data)
if hasattr(self, '_coerce'):
- data = self._coerce(data)
- initial_value = self._coerce(initial_value)
+ return self._coerce(data) != self._coerce(initial)
except ValidationError:
return True
+ # For purposes of seeing whether something has changed, None is
+ # the same as an empty string, if the data or initial value we get
+ # is None, replace it with ''.
+ initial_value = initial if initial is not None else ''
data_value = data if data is not None else ''
return initial_value != data_value
diff --git a/docs/releases/1.9.1.txt b/docs/releases/1.9.1.txt
index 25acbd540b..20fd723d2b 100644
--- a/docs/releases/1.9.1.txt
+++ b/docs/releases/1.9.1.txt
@@ -43,3 +43,6 @@ Bugfixes
* Fixed a state bug when using an ``AlterModelManagers`` operation
(:ticket:`25852`).
+
+* Fixed ``TypedChoiceField`` change detection with nullable fields
+ (:ticket:`25942`).
diff --git a/tests/forms_tests/tests/test_fields.py b/tests/forms_tests/tests/test_fields.py
index 42e7bc91f7..7fae0bdfc2 100644
--- a/tests/forms_tests/tests/test_fields.py
+++ b/tests/forms_tests/tests/test_fields.py
@@ -1246,6 +1246,14 @@ class FieldsTests(SimpleTestCase):
self.assertFalse(f.has_changed(1, '1'))
self.assertFalse(f.has_changed('1', '1'))
+ f = TypedChoiceField(
+ choices=[('', '---------'), ('a', "a"), ('b', "b")], coerce=six.text_type,
+ required=False, initial=None, empty_value=None,
+ )
+ self.assertFalse(f.has_changed(None, ''))
+ self.assertTrue(f.has_changed('', 'a'))
+ self.assertFalse(f.has_changed('a', 'a'))
+
def test_typedchoicefield_special_coerce(self):
"""
Test a coerce function which results in a value not present in choices.