summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Graham <timograham@gmail.com>2015-08-07 13:06:56 -0400
committerTim Graham <timograham@gmail.com>2015-08-07 14:31:05 -0400
commitad2ac53054f1deea6818d05220db73f6c09c1702 (patch)
treea2902d1a42e33a7cb96adba2dffc812b692cd2c8
parenta7fb311aced5469dbf2588d860579be87b67e3d9 (diff)
[1.8.x] Fixed #25233 -- Fixed HStoreField.has_changed() handling of initial values.
Thanks Simon Charette for review. Backport of a7b7f27c05244d69a11545261eb3bbd73791b3d2 from master
-rw-r--r--django/contrib/postgres/forms/hstore.py15
-rw-r--r--docs/releases/1.8.4.txt4
-rw-r--r--tests/postgres_tests/test_hstore.py6
3 files changed, 16 insertions, 9 deletions
diff --git a/django/contrib/postgres/forms/hstore.py b/django/contrib/postgres/forms/hstore.py
index 3dc70ab2ab..bf562c0e8f 100644
--- a/django/contrib/postgres/forms/hstore.py
+++ b/django/contrib/postgres/forms/hstore.py
@@ -23,13 +23,14 @@ class HStoreField(forms.CharField):
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)
diff --git a/docs/releases/1.8.4.txt b/docs/releases/1.8.4.txt
index f95bcfb4dc..070397e885 100644
--- a/docs/releases/1.8.4.txt
+++ b/docs/releases/1.8.4.txt
@@ -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`).
diff --git a/tests/postgres_tests/test_hstore.py b/tests/postgres_tests/test_hstore.py
index aa34773d1e..f5e6f416ad 100644
--- a/tests/postgres_tests/test_hstore.py
+++ b/tests/postgres_tests/test_hstore.py
@@ -196,6 +196,12 @@ class TestFormField(TestCase):
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(TestCase):