fix FilePathField required argument (#8805)

This commit is contained in:
Radosław Kowalski 2023-01-04 20:21:57 +01:00 committed by GitHub
parent 069c701ebf
commit 89d6ce7d43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 1 deletions

View File

@ -1480,6 +1480,7 @@ class FilePathField(ChoiceField):
allow_folders=allow_folders, required=required
)
kwargs['choices'] = field.choices
kwargs['required'] = required
super().__init__(**kwargs)

View File

@ -15,7 +15,8 @@ from django.utils.timezone import activate, deactivate, override
import rest_framework
from rest_framework import exceptions, serializers
from rest_framework.fields import (
BuiltinSignatureError, DjangoImageField, is_simple_callable
BuiltinSignatureError, DjangoImageField, SkipField, empty,
is_simple_callable
)
from tests.models import UUIDForeignKeyTarget
@ -2390,6 +2391,21 @@ class TestFileFieldContext:
assert value == 'http://example.com/example.txt'
# Tests for FilePathField.
# --------------------
class TestFilePathFieldRequired:
def test_required_passed_to_both_django_file_path_field_and_base(self):
field = serializers.FilePathField(
path=os.path.abspath(os.path.dirname(__file__)),
required=False,
)
assert "" in field.choices # Django adds empty choice if not required
assert field.required is False
with pytest.raises(SkipField):
field.run_validation(empty)
# Tests for SerializerMethodField.
# --------------------------------