diff --git a/rest_framework/utils/field_mapping.py b/rest_framework/utils/field_mapping.py index 1281ee167..b90c3eead 100644 --- a/rest_framework/utils/field_mapping.py +++ b/rest_framework/utils/field_mapping.py @@ -109,6 +109,9 @@ def get_field_kwargs(field_name, model_field): if model_field.blank and (isinstance(model_field, (models.CharField, models.TextField))): kwargs['allow_blank'] = True + if not model_field.blank and (postgres_fields and isinstance(model_field, postgres_fields.ArrayField)): + kwargs['allow_empty'] = False + if isinstance(model_field, models.FilePathField): kwargs['path'] = model_field.path diff --git a/tests/test_model_serializer.py b/tests/test_model_serializer.py index ae320927c..4d9aff454 100644 --- a/tests/test_model_serializer.py +++ b/tests/test_model_serializer.py @@ -440,15 +440,17 @@ class TestPosgresFieldsMapping(TestCase): def test_array_field(self): class ArrayFieldModel(models.Model): array_field = postgres_fields.ArrayField(base_field=models.CharField()) + array_field_with_blank = postgres_fields.ArrayField(blank=True, base_field=models.CharField()) class TestSerializer(serializers.ModelSerializer): class Meta: model = ArrayFieldModel - fields = ['array_field'] + fields = ['array_field', 'array_field_with_blank'] expected = dedent(""" TestSerializer(): - array_field = ListField(child=CharField(label='Array field', validators=[])) + array_field = ListField(allow_empty=False, child=CharField(label='Array field', validators=[])) + array_field_with_blank = ListField(child=CharField(label='Array field with blank', validators=[]), required=False) """) self.assertEqual(repr(TestSerializer()), expected)