diff --git a/django/contrib/postgres/forms/hstore.py b/django/contrib/postgres/forms/hstore.py index 2c564ea53c..a138093e90 100644 --- a/django/contrib/postgres/forms/hstore.py +++ b/django/contrib/postgres/forms/hstore.py @@ -34,3 +34,13 @@ class HStoreField(forms.CharField): for key, val in value.items(): value[key] = six.text_type(val) return value + + def has_changed(self, initial, data): + """ + Return True if data differs from initial. + """ + # For purposes of seeing whether something has changed, None is + # the same as an empty dict, if the data or initial value we get + # is None, replace it w/ {}. + initial_value = self.to_python(initial) + return super(forms.HStoreField, self).has_changed(initial_value, data) diff --git a/docs/releases/1.8.3.txt b/docs/releases/1.8.3.txt index d5bde6040c..1155ac1cb6 100644 --- a/docs/releases/1.8.3.txt +++ b/docs/releases/1.8.3.txt @@ -15,5 +15,7 @@ Bugfixes * Fixed crash during :djadmin:`makemigrations` if a migrations module either is missing ``__init__.py`` or is a file (:ticket:`24848`). -* Fixed ``exists()`` returning incorrect results after annotation with +* Fixed ``QuerySet.exists()`` returning incorrect results after annotation with ``Count()`` (:ticket:`24835`). + +* Corrected ``HStoreField.has_changed()`` (:ticket:`24844`). diff --git a/tests/postgres_tests/test_hstore.py b/tests/postgres_tests/test_hstore.py index 8b2cc883dc..b36333d90c 100644 --- a/tests/postgres_tests/test_hstore.py +++ b/tests/postgres_tests/test_hstore.py @@ -4,6 +4,7 @@ from django.contrib.postgres import forms from django.contrib.postgres.fields import HStoreField from django.contrib.postgres.validators import KeysValidator from django.core import exceptions, serializers +from django.forms import Form from django.test import TestCase from .models import HStoreModel @@ -174,6 +175,12 @@ class TestFormField(TestCase): form_field = model_field.formfield() self.assertIsInstance(form_field, forms.HStoreField) + def test_empty_field_has_not_changed(self): + class HStoreFormTest(Form): + f1 = HStoreField() + form_w_hstore = HStoreFormTest() + self.assertFalse(form_w_hstore.has_changed()) + class TestValidator(TestCase):