From c238e65e29494042d6ccf925c0a06a3135619799 Mon Sep 17 00:00:00 2001 From: Chason Chaffin Date: Wed, 3 Jul 2019 18:32:43 +0900 Subject: [PATCH] Fixed #30596 -- Fixed SplitArrayField.has_changed() for non-string base fields. Thanks to Evgeniy Krysanov for the report and the idea to use to_python. Thanks to Mariusz Felisiak for the test case. --- django/contrib/postgres/forms/array.py | 4 ++++ tests/postgres_tests/test_array.py | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/django/contrib/postgres/forms/array.py b/django/contrib/postgres/forms/array.py index 554a39d4f6..3de1fd86cb 100644 --- a/django/contrib/postgres/forms/array.py +++ b/django/contrib/postgres/forms/array.py @@ -178,6 +178,10 @@ class SplitArrayField(forms.Field): kwargs.setdefault('widget', widget) super().__init__(**kwargs) + def to_python(self, value): + value = super().to_python(value) + return [self.base_field.to_python(item) for item in value] + def clean(self, value): cleaned_data = [] errors = [] diff --git a/tests/postgres_tests/test_array.py b/tests/postgres_tests/test_array.py index d7ed5223e3..170c1caa0a 100644 --- a/tests/postgres_tests/test_array.py +++ b/tests/postgres_tests/test_array.py @@ -909,6 +909,18 @@ class TestSplitFormField(PostgreSQLSimpleTestCase): obj = form.save(commit=False) self.assertEqual(obj.field, [1, 2]) + def test_splitarrayfield_has_changed(self): + class Form(forms.ModelForm): + field = SplitArrayField(forms.IntegerField(), required=False, size=2) + + class Meta: + model = IntegerArrayModel + fields = ('field',) + + obj = IntegerArrayModel(field=[1, 2]) + form = Form({'field_0': '1', 'field_1': '2'}, instance=obj) + self.assertFalse(form.has_changed()) + class TestSplitFormWidget(PostgreSQLWidgetTestCase):