From 321133242f40e18b3d7564a43a3b515c17f3e40c Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Wed, 24 Jun 2015 14:24:19 +0100 Subject: [PATCH] Unsaved file objects may not have an associated URL. --- rest_framework/fields.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index ff6db703f..275c7a9a7 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -1198,8 +1198,12 @@ class FileField(Field): return data def to_representation(self, value): + if not value: + return None + if self.use_url: - if not value: + if not getattr(value, 'url', None): + # If the file has not been saved it may not have a URL. return None url = value.url request = self.context.get('request', None)