Support empty file fields

This commit is contained in:
Tom Christie 2014-10-08 16:59:52 +01:00
parent 4c015df28c
commit 5ead8dc89d
2 changed files with 4 additions and 1 deletions

View File

@ -913,6 +913,8 @@ class FileField(Field):
def to_representation(self, value):
if self.use_url:
if not value:
return None
url = settings.MEDIA_URL + value.url
request = self.context.get('request', None)
if request is not None:

View File

@ -879,7 +879,8 @@ class TestFileField(FieldValues):
(MockFile(name='x' * 100, size=10), ['Ensure this filename has at most 10 characters (it has 100).'])
]
outputs = [
(MockFile(name='example.txt', url='/example.txt'), '/example.txt')
(MockFile(name='example.txt', url='/example.txt'), '/example.txt'),
('', None)
]
field = fields.FileField(max_length=10)