Skip to content

Commit

Permalink
Fixed #25233 -- Fixed HStoreField.has_changed() handling of initial v…
Browse files Browse the repository at this point in the history
…alues.

Thanks Simon Charette for review.
  • Loading branch information
timgraham committed Aug 7, 2015
1 parent 6ed613b commit a7b7f27
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
15 changes: 8 additions & 7 deletions django/contrib/postgres/forms/hstore.py
Expand Up @@ -23,13 +23,14 @@ def prepare_value(self, value):
def to_python(self, value):
if not value:
return {}
try:
value = json.loads(value)
except ValueError:
raise ValidationError(
self.error_messages['invalid_json'],
code='invalid_json',
)
if not isinstance(value, dict):
try:
value = json.loads(value)
except ValueError:
raise ValidationError(
self.error_messages['invalid_json'],
code='invalid_json',
)
# Cast everything to strings for ease.
for key, val in value.items():
value[key] = six.text_type(val)
Expand Down
4 changes: 2 additions & 2 deletions docs/releases/1.8.4.txt
Expand Up @@ -22,5 +22,5 @@ Bugfixes
* Prevented an exception in ``TestCase.setUpTestData()`` from leaking the
transaction (:ticket:`25176`).

* Fixed ``has_changed()`` method in
:class:`django.contrib.postgres.forms.HStoreField`.
* Fixed ``has_changed()`` method in ``contrib.postgres.forms.HStoreField``
(:ticket:`25215`, :ticket:`25233`).
6 changes: 6 additions & 0 deletions tests/postgres_tests/test_hstore.py
Expand Up @@ -206,6 +206,12 @@ class HStoreFormTest(Form):
form_w_hstore = HStoreFormTest({'f1': '{"a": 2}'}, initial={'f1': '{"a": 1}'})
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):

Expand Down

0 comments on commit a7b7f27

Please sign in to comment.