Fix error when serializer gets files but no data

This commit is contained in:
Philip Douglas 2013-09-12 16:03:20 +01:00
parent 39e13a0d13
commit 59cce01b33
2 changed files with 14 additions and 1 deletions

View File

@ -156,7 +156,7 @@ class BaseSerializer(WritableField):
self.context = context or {} self.context = context or {}
self.init_data = data self.init_data = data or {}
self.init_files = files self.init_files = files
self.object = instance self.object = instance
self.fields = self.get_fields() self.fields = self.get_fields()

View File

@ -80,3 +80,16 @@ class FileSerializerTests(TestCase):
serializer = UploadedFileSerializer(data={'created': now, 'file': 'abc'}) serializer = UploadedFileSerializer(data={'created': now, 'file': 'abc'})
self.assertFalse(serializer.is_valid()) self.assertFalse(serializer.is_valid())
self.assertEqual(serializer.errors, {'file': [errmsg]}) self.assertEqual(serializer.errors, {'file': [errmsg]})
def test_validation_with_no_data(self):
"""
Validation should still function when no data dictionary is provided.
"""
now = datetime.datetime.now()
file = BytesIO(six.b('stuff'))
file.name = 'stuff.txt'
file.size = len(file.getvalue())
uploaded_file = UploadedFile(file=file, created=now)
serializer = UploadedFileSerializer(files={'file': file})
self.assertFalse(serializer.is_valid())