mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-27 03:54:01 +03:00
Fix error when serializer gets files but no data
This commit is contained in:
parent
39e13a0d13
commit
59cce01b33
|
@ -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()
|
||||||
|
|
|
@ -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())
|
Loading…
Reference in New Issue
Block a user