diff --git a/docs/api-guide/fields.md b/docs/api-guide/fields.md index 8d25d6c78..d18347803 100644 --- a/docs/api-guide/fields.md +++ b/docs/api-guide/fields.md @@ -417,6 +417,7 @@ Corresponds to `django.forms.fields.FileField`. - `max_length` - Designates the maximum length for the file name. - `allow_empty_file` - Designates if empty files are allowed. - `use_url` - If set to `True` then URL string values will be used for the output representation. If set to `False` then filename string values will be used for the output representation. Defaults to the value of the `UPLOADED_FILES_USE_URL` settings key, which is `True` unless set otherwise. +- `use_prefix` - It will replace the original schema and host and be effective only when `use_url` is `True`. If set to a non-empty string and should include url schema and location, such as `http://192.168.1.1`. Defaults to the value of the `UPLOADED_FILES_USE_PREFIX` settings key, which is an empty string and means doing nothing. ## ImageField @@ -429,6 +430,7 @@ Corresponds to `django.forms.fields.ImageField`. - `max_length` - Designates the maximum length for the file name. - `allow_empty_file` - Designates if empty files are allowed. - `use_url` - If set to `True` then URL string values will be used for the output representation. If set to `False` then filename string values will be used for the output representation. Defaults to the value of the `UPLOADED_FILES_USE_URL` settings key, which is `True` unless set otherwise. +- `use_prefix` - It will replace the original schema and host and be effective only when `use_url` is `True`. If set to a non-empty string and should include url schema and location, such as `http://192.168.1.1`. Defaults to the value of the `UPLOADED_FILES_USE_PREFIX` settings key, which is an empty string and means doing nothing. Requires either the `Pillow` package or `PIL` package. The `Pillow` package is recommended, as `PIL` is no longer actively maintained. diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 3378de368..6820e6b03 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -1531,6 +1531,8 @@ class FileField(Field): self.allow_empty_file = kwargs.pop('allow_empty_file', False) if 'use_url' in kwargs: self.use_url = kwargs.pop('use_url') + if 'use_prefix' in kwargs: + self.use_prefix = kwargs.pop('use_prefix') super(FileField, self).__init__(*args, **kwargs) def to_internal_value(self, data): @@ -1555,6 +1557,7 @@ class FileField(Field): return None use_url = getattr(self, 'use_url', api_settings.UPLOADED_FILES_USE_URL) + prefix = getattr(self, 'use_prefix', api_settings.UPLOADED_FILES_USE_PREFIX) if use_url: if not getattr(value, 'url', None): @@ -1563,6 +1566,8 @@ class FileField(Field): url = value.url request = self.context.get('request', None) if request is not None: + if prefix: + url = prefix + url return request.build_absolute_uri(url) return url return value.name diff --git a/rest_framework/settings.py b/rest_framework/settings.py index 6c581f8e8..90f629292 100644 --- a/rest_framework/settings.py +++ b/rest_framework/settings.py @@ -117,6 +117,7 @@ DEFAULTS = { 'STRICT_JSON': True, 'COERCE_DECIMAL_TO_STRING': True, 'UPLOADED_FILES_USE_URL': True, + 'UPLOADED_FILES_USE_PREFIX': '', # Browseable API 'HTML_SELECT_CUTOFF': 1000,