mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-10 19:56:59 +03:00
Fix ArrayField kwargs mapping for blank/allow_empty (#6758)
Postgres ArrayField blank=True should allow empty Lists in Serializer
This commit is contained in:
parent
3242adf058
commit
a7778897ad
|
@ -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
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user