Fixed #28534 -- Made JSONField.has_changed() ignore key order and consider True/1 values as different.

This commit is contained in:
shanghui 2017-09-01 14:26:09 +08:00 committed by Tim Graham
parent bdb747a5f2
commit 1907fc9b12
2 changed files with 13 additions and 0 deletions

View File

@ -52,3 +52,11 @@ class JSONField(forms.CharField):
if isinstance(value, InvalidJSONInput):
return value
return json.dumps(value)
def has_changed(self, initial, data):
if super().has_changed(initial, data):
return True
# For purposes of seeing whether something has changed, True isn't the
# same as 1 and the order of keys doesn't matter.
data = self.to_python(data)
return json.dumps(initial, sort_keys=True) != json.dumps(data, sort_keys=True)

View File

@ -410,3 +410,8 @@ class TestFormField(PostgreSQLTestCase):
for json_string in tests:
val = field.clean(json_string)
self.assertEqual(field.clean(val), val)
def test_has_changed(self):
field = forms.JSONField()
self.assertIs(field.has_changed({'a': True}, '{"a": 1}'), True)
self.assertIs(field.has_changed({'a': 1, 'b': 2}, '{"b": 2, "a": 1}'), False)