Fixed #24844 -- Corrected has_changed implementation for HStoreField.

Backport of 43b2d88a5b from master
This commit is contained in:
Andrea Grandi 2015-05-24 15:56:48 +01:00 committed by Tim Graham
parent 2aa2b9f291
commit 62c19a21b6
3 changed files with 20 additions and 1 deletions

View File

@ -34,3 +34,13 @@ class HStoreField(forms.CharField):
for key, val in value.items(): for key, val in value.items():
value[key] = six.text_type(val) value[key] = six.text_type(val)
return value 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)

View File

@ -15,5 +15,7 @@ Bugfixes
* Fixed crash during :djadmin:`makemigrations` if a migrations module either * Fixed crash during :djadmin:`makemigrations` if a migrations module either
is missing ``__init__.py`` or is a file (:ticket:`24848`). 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`). ``Count()`` (:ticket:`24835`).
* Corrected ``HStoreField.has_changed()`` (:ticket:`24844`).

View File

@ -4,6 +4,7 @@ from django.contrib.postgres import forms
from django.contrib.postgres.fields import HStoreField from django.contrib.postgres.fields import HStoreField
from django.contrib.postgres.validators import KeysValidator from django.contrib.postgres.validators import KeysValidator
from django.core import exceptions, serializers from django.core import exceptions, serializers
from django.forms import Form
from django.test import TestCase from django.test import TestCase
from .models import HStoreModel from .models import HStoreModel
@ -174,6 +175,12 @@ class TestFormField(TestCase):
form_field = model_field.formfield() form_field = model_field.formfield()
self.assertIsInstance(form_field, forms.HStoreField) 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): class TestValidator(TestCase):