Commit 62c19a21 authored by Andrea Grandi's avatar Andrea Grandi Committed by Tim Graham
Browse files

Fixed #24844 -- Corrected has_changed implementation for HStoreField.

Backport of 43b2d88a from master
parent 2aa2b9f2
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -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)
+3 −1
Original line number Diff line number Diff line
@@ -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 −0
Original line number Diff line number Diff line
@@ -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):