From 6a1957bb9887f8e0bb69d3d2b180a1414d38c1fc Mon Sep 17 00:00:00 2001 From: Vinay Karanam Date: Wed, 3 Jan 2018 12:10:14 +0530 Subject: [PATCH] Fixed #28950 -- Fixed ArrayField.has_changed() for empty values. --- django/contrib/postgres/forms/array.py | 10 ++++++++++ tests/postgres_tests/test_array.py | 8 ++++++++ 2 files changed, 18 insertions(+) diff --git a/django/contrib/postgres/forms/array.py b/django/contrib/postgres/forms/array.py index f01d5836c1..d9e3256e1f 100644 --- a/django/contrib/postgres/forms/array.py +++ b/django/contrib/postgres/forms/array.py @@ -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' diff --git a/tests/postgres_tests/test_array.py b/tests/postgres_tests/test_array.py index 71d0603469..cc264fe9fc 100644 --- a/tests/postgres_tests/test_array.py +++ b/tests/postgres_tests/test_array.py @@ -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):