diff --git a/rest_framework/compat.py b/rest_framework/compat.py index d11c68093..07f0741ac 100644 --- a/rest_framework/compat.py +++ b/rest_framework/compat.py @@ -12,6 +12,7 @@ import django from django.conf import settings from django.core.exceptions import ImproperlyConfigured from django.db import connection, transaction +from django.forms import FilePathField as DjangoFilePathField from django.test.client import FakePayload from django.utils import six from django.utils.encoding import force_text @@ -291,3 +292,21 @@ def set_rollback(): else: # transaction not managed pass + + +def get_filepathfield(path, match=None, recursive=False, allow_files=True, + allow_folders=False, required=None): + """Create proper Django FilePathField with allowed kwargs.""" + + if django.VERSION < (1, 5): + # django field doesn't have allow_folders, allow_files kwargs + field = DjangoFilePathField( + path, match=match, recursive=recursive, required=required + ) + else: + field = DjangoFilePathField( + path, match=match, recursive=recursive, allow_files=allow_files, + allow_folders=allow_folders, required=required + ) + + return field diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 350e49576..bf3930724 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -26,7 +26,7 @@ from rest_framework import ISO_8601 from rest_framework.compat import ( EmailValidator, MaxLengthValidator, MaxValueValidator, MinLengthValidator, MinValueValidator, OrderedDict, URLValidator, duration_string, - parse_duration, unicode_repr, unicode_to_repr + parse_duration, unicode_repr, unicode_to_repr, get_filepathfield ) from rest_framework.exceptions import ValidationError from rest_framework.settings import api_settings @@ -717,16 +717,10 @@ class FilePathField(CharField): super(FilePathField, self).__init__(**kwargs) # create field and get options to avoid code duplication - if django.VERSION < (1, 5): - # django field doesn't have allow_folders, allow_files kwargs - field = DjangoFilePathField( - path, match=match, recursive=recursive, required=required - ) - else: - field = DjangoFilePathField( - path, match=match, recursive=recursive, allow_files=allow_files, - allow_folders=allow_folders, required=required - ) + field = get_filepathfield( + path, match=match, recursive=recursive, allow_files=allow_files, + allow_folders=allow_folders, required=required + ) self.choices = OrderedDict(field.choices) self.choice_strings_to_values = dict([ diff --git a/rest_framework/filters.py b/rest_framework/filters.py index a5e06b574..6cbaae16e 100644 --- a/rest_framework/filters.py +++ b/rest_framework/filters.py @@ -191,6 +191,7 @@ class DjangoObjectPermissionsFilter(BaseFilterBackend): perm_format = '%(app_label)s.view_%(model_name)s' def filter_queryset(self, request, queryset, view): + extra = {} user = request.user model_cls = queryset.model kwargs = { diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index fa3a53374..6a6eb4d9f 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -773,6 +773,7 @@ class ModelSerializer(Serializer): models.TimeField: TimeField, models.URLField: URLField, models.GenericIPAddressField: IPAddressField, + models.FilePathField: FilePathField, } if ModelDurationField is not None: serializer_field_mapping[ModelDurationField] = DurationField