mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-22 09:36:49 +03:00
fix FilePathField required argument (#8805)
This commit is contained in:
parent
069c701ebf
commit
89d6ce7d43
|
@ -1480,6 +1480,7 @@ class FilePathField(ChoiceField):
|
||||||
allow_folders=allow_folders, required=required
|
allow_folders=allow_folders, required=required
|
||||||
)
|
)
|
||||||
kwargs['choices'] = field.choices
|
kwargs['choices'] = field.choices
|
||||||
|
kwargs['required'] = required
|
||||||
super().__init__(**kwargs)
|
super().__init__(**kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,8 @@ from django.utils.timezone import activate, deactivate, override
|
||||||
import rest_framework
|
import rest_framework
|
||||||
from rest_framework import exceptions, serializers
|
from rest_framework import exceptions, serializers
|
||||||
from rest_framework.fields import (
|
from rest_framework.fields import (
|
||||||
BuiltinSignatureError, DjangoImageField, is_simple_callable
|
BuiltinSignatureError, DjangoImageField, SkipField, empty,
|
||||||
|
is_simple_callable
|
||||||
)
|
)
|
||||||
from tests.models import UUIDForeignKeyTarget
|
from tests.models import UUIDForeignKeyTarget
|
||||||
|
|
||||||
|
@ -2390,6 +2391,21 @@ class TestFileFieldContext:
|
||||||
assert value == 'http://example.com/example.txt'
|
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.
|
# Tests for SerializerMethodField.
|
||||||
# --------------------------------
|
# --------------------------------
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user