Skip to content

Commit 89d6ce7

Browse files
authored
fix FilePathField required argument (#8805)
1 parent 069c701 commit 89d6ce7

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

rest_framework/fields.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,6 +1480,7 @@ def __init__(self, path, match=None, recursive=False, allow_files=True,
14801480
allow_folders=allow_folders, required=required
14811481
)
14821482
kwargs['choices'] = field.choices
1483+
kwargs['required'] = required
14831484
super().__init__(**kwargs)
14841485

14851486

tests/test_fields.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
import rest_framework
1616
from rest_framework import exceptions, serializers
1717
from rest_framework.fields import (
18-
BuiltinSignatureError, DjangoImageField, is_simple_callable
18+
BuiltinSignatureError, DjangoImageField, SkipField, empty,
19+
is_simple_callable
1920
)
2021
from tests.models import UUIDForeignKeyTarget
2122

@@ -2390,6 +2391,21 @@ def test_fully_qualified_when_request_in_context(self):
23902391
assert value == 'http://example.com/example.txt'
23912392

23922393

2394+
# Tests for FilePathField.
2395+
# --------------------
2396+
2397+
class TestFilePathFieldRequired:
2398+
def test_required_passed_to_both_django_file_path_field_and_base(self):
2399+
field = serializers.FilePathField(
2400+
path=os.path.abspath(os.path.dirname(__file__)),
2401+
required=False,
2402+
)
2403+
assert "" in field.choices # Django adds empty choice if not required
2404+
assert field.required is False
2405+
with pytest.raises(SkipField):
2406+
field.run_validation(empty)
2407+
2408+
23932409
# Tests for SerializerMethodField.
23942410
# --------------------------------
23952411

0 commit comments

Comments
 (0)