diff options
| author | Romulo Furtado <romulo.furtado@loggi.com> | 2017-04-06 11:06:11 -0300 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2017-06-13 19:44:24 -0400 |
| commit | 9dd244394236388c3479ab202a0ec31055f7ec09 (patch) | |
| tree | 9190be9fc08c17df3e3788be3be5c408b32298da | |
| parent | de9294727c95d6acec82f335c8517930c2f211b1 (diff) | |
Fixed #27161 -- Fixed form validation when an ArrayField's base_field has choices.
| -rw-r--r-- | django/contrib/postgres/forms/array.py | 4 | ||||
| -rw-r--r-- | tests/postgres_tests/test_array.py | 5 |
2 files changed, 9 insertions, 0 deletions
diff --git a/django/contrib/postgres/forms/array.py b/django/contrib/postgres/forms/array.py index 6b3b217810..71c0476fa8 100644 --- a/django/contrib/postgres/forms/array.py +++ b/django/contrib/postgres/forms/array.py @@ -27,6 +27,10 @@ class SimpleArrayField(forms.CharField): self.max_length = max_length self.validators.append(ArrayMaxLengthValidator(int(max_length))) + def clean(self, value): + value = super().clean(value) + return [self.base_field.clean(val) for val in value] + def prepare_value(self, value): if isinstance(value, list): return self.delimiter.join(str(self.base_field.prepare_value(v)) for v in value) diff --git a/tests/postgres_tests/test_array.py b/tests/postgres_tests/test_array.py index a66c92a016..e2e4ccdeb2 100644 --- a/tests/postgres_tests/test_array.py +++ b/tests/postgres_tests/test_array.py @@ -686,6 +686,11 @@ class TestSimpleFormField(PostgreSQLTestCase): self.assertIsInstance(form_field, SimpleArrayField) self.assertEqual(form_field.max_length, 4) + def test_model_field_choices(self): + model_field = ArrayField(models.IntegerField(choices=((1, 'A'), (2, 'B')))) + form_field = model_field.formfield() + self.assertEqual(form_field.clean('1,2'), [1, 2]) + def test_already_converted_value(self): field = SimpleArrayField(forms.CharField()) vals = ['a', 'b', 'c'] |
