Fixed #28950 -- Fixed ArrayField.has_changed() for empty values.

This commit is contained in:
Vinay Karanam 2018-01-03 12:10:14 +05:30 committed by Tim Graham
parent e67dc0fbb2
commit 6a1957bb98
2 changed files with 18 additions and 0 deletions

View File

@ -91,6 +91,16 @@ class SimpleArrayField(forms.CharField):
if errors:
raise ValidationError(errors)
def has_changed(self, initial, data):
try:
value = self.to_python(data)
except ValidationError:
pass
else:
if initial in self.empty_values and value in self.empty_values:
return False
return super().has_changed(initial, data)
class SplitArrayWidget(forms.Widget):
template_name = 'postgres/widgets/split_array.html'

View File

@ -760,6 +760,14 @@ class TestSimpleFormField(PostgreSQLTestCase):
self.assertIs(field.has_changed([1, 2], '1,2,3'), True)
self.assertIs(field.has_changed([1, 2], 'a,b'), True)
def test_has_changed_empty(self):
field = SimpleArrayField(forms.CharField())
self.assertIs(field.has_changed(None, None), False)
self.assertIs(field.has_changed(None, ''), False)
self.assertIs(field.has_changed(None, []), False)
self.assertIs(field.has_changed([], None), False)
self.assertIs(field.has_changed([], ''), False)
class TestSplitFormField(PostgreSQLTestCase):