Fixed #25233 -- Fixed HStoreField.has_changed() handling of initial values.

Thanks Simon Charette for review.
This commit is contained in:
Tim Graham 2015-08-07 13:06:56 -04:00
parent 6ed613b2a5
commit a7b7f27c05
3 changed files with 16 additions and 9 deletions

View File

@ -23,13 +23,14 @@ class HStoreField(forms.CharField):
def to_python(self, value): def to_python(self, value):
if not value: if not value:
return {} return {}
try: if not isinstance(value, dict):
value = json.loads(value) try:
except ValueError: value = json.loads(value)
raise ValidationError( except ValueError:
self.error_messages['invalid_json'], raise ValidationError(
code='invalid_json', self.error_messages['invalid_json'],
) code='invalid_json',
)
# Cast everything to strings for ease. # Cast everything to strings for ease.
for key, val in value.items(): for key, val in value.items():
value[key] = six.text_type(val) value[key] = six.text_type(val)

View File

@ -22,5 +22,5 @@ Bugfixes
* Prevented an exception in ``TestCase.setUpTestData()`` from leaking the * Prevented an exception in ``TestCase.setUpTestData()`` from leaking the
transaction (:ticket:`25176`). transaction (:ticket:`25176`).
* Fixed ``has_changed()`` method in * Fixed ``has_changed()`` method in ``contrib.postgres.forms.HStoreField``
:class:`django.contrib.postgres.forms.HStoreField`. (:ticket:`25215`, :ticket:`25233`).

View File

@ -206,6 +206,12 @@ class TestFormField(PostgreSQLTestCase):
form_w_hstore = HStoreFormTest({'f1': '{"a": 2}'}, initial={'f1': '{"a": 1}'}) form_w_hstore = HStoreFormTest({'f1': '{"a": 2}'}, initial={'f1': '{"a": 1}'})
self.assertTrue(form_w_hstore.has_changed()) self.assertTrue(form_w_hstore.has_changed())
form_w_hstore = HStoreFormTest({'f1': '{"a": 1}'}, initial={'f1': {"a": 1}})
self.assertFalse(form_w_hstore.has_changed())
form_w_hstore = HStoreFormTest({'f1': '{"a": 2}'}, initial={'f1': {"a": 1}})
self.assertTrue(form_w_hstore.has_changed())
class TestValidator(PostgreSQLTestCase): class TestValidator(PostgreSQLTestCase):