Fix ArrayField kwargs mapping for blank/allow_empty (#6758)

Postgres ArrayField blank=True should allow empty Lists in Serializer
This commit is contained in:
Karambir Singh Nain 2019-07-02 06:04:34 +05:30 committed by Ryan P Kilby
parent 3242adf058
commit a7778897ad
2 changed files with 7 additions and 2 deletions

View File

@ -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

View File

@ -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=[<django.core.validators.MaxLengthValidator object>]))
array_field = ListField(allow_empty=False, child=CharField(label='Array field', validators=[<django.core.validators.MaxLengthValidator object>]))
array_field_with_blank = ListField(child=CharField(label='Array field with blank', validators=[<django.core.validators.MaxLengthValidator object>]), required=False)
""")
self.assertEqual(repr(TestSerializer()), expected)