Skip to content

Commit

Permalink
Fixed #24844 -- Corrected has_changed implementation for HStoreField.
Browse files Browse the repository at this point in the history
Backport of 43b2d88 from master
  • Loading branch information
andreagrandi authored and timgraham committed May 26, 2015
1 parent 2aa2b9f commit 62c19a2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
10 changes: 10 additions & 0 deletions django/contrib/postgres/forms/hstore.py
Expand Up @@ -34,3 +34,13 @@ def to_python(self, value):
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)
4 changes: 3 additions & 1 deletion docs/releases/1.8.3.txt
Expand Up @@ -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`).
7 changes: 7 additions & 0 deletions tests/postgres_tests/test_hstore.py
Expand Up @@ -4,6 +4,7 @@
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
Expand Down Expand Up @@ -174,6 +175,12 @@ def test_model_field_formfield(self):
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):

Expand Down

0 comments on commit 62c19a2

Please sign in to comment.