From 59cce01b3359aa009e697a99eabbf2ef322b28e2 Mon Sep 17 00:00:00 2001 From: Philip Douglas Date: Thu, 12 Sep 2013 16:03:20 +0100 Subject: [PATCH] Fix error when serializer gets files but no data --- rest_framework/serializers.py | 2 +- rest_framework/tests/test_files.py | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index a63c7f6c2..778e72d1a 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -156,7 +156,7 @@ class BaseSerializer(WritableField): self.context = context or {} - self.init_data = data + self.init_data = data or {} self.init_files = files self.object = instance self.fields = self.get_fields() diff --git a/rest_framework/tests/test_files.py b/rest_framework/tests/test_files.py index c13c38b86..127e379e9 100644 --- a/rest_framework/tests/test_files.py +++ b/rest_framework/tests/test_files.py @@ -80,3 +80,16 @@ class FileSerializerTests(TestCase): serializer = UploadedFileSerializer(data={'created': now, 'file': 'abc'}) self.assertFalse(serializer.is_valid()) 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()) \ No newline at end of file