diff --git a/rest_framework/fields.py b/rest_framework/fields.py index e1fd1b646..86c3a837a 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -472,7 +472,7 @@ class DateField(WritableField): parsed = parse_date(value) if parsed is not None: return parsed - except ValueError: + except (ValueError, TypeError): msg = self.error_messages['invalid_date'] % value raise ValidationError(msg) @@ -520,7 +520,7 @@ class DateTimeField(WritableField): parsed = parse_datetime(value) if parsed is not None: return parsed - except ValueError: + except (ValueError, TypeError): msg = self.error_messages['invalid_datetime'] % value raise ValidationError(msg) @@ -528,7 +528,7 @@ class DateTimeField(WritableField): parsed = parse_date(value) if parsed is not None: return datetime.datetime(parsed.year, parsed.month, parsed.day) - except ValueError: + except (ValueError, TypeError): msg = self.error_messages['invalid_date'] % value raise ValidationError(msg) @@ -558,7 +558,7 @@ class TimeField(WritableField): parsed = parse_time(value) assert parsed is not None return parsed - except ValueError: + except (ValueError, TypeError): msg = self.error_messages['invalid'] % value raise ValidationError(msg) diff --git a/rest_framework/tests/serializer.py b/rest_framework/tests/serializer.py index da1101386..e85323e0c 100644 --- a/rest_framework/tests/serializer.py +++ b/rest_framework/tests/serializer.py @@ -338,6 +338,21 @@ class ValidationTests(TestCase): self.assertEquals(serializer.is_valid(), False) self.assertEquals(serializer.errors, {'info': ['Ensure this value has at most 12 characters (it has 13).']}) + def test_datetime_validation_failure(self): + """ + Test DateTimeField validation errors on non-str values. + Regression test for #669. + + https://github.com/tomchristie/django-rest-framework/issues/669 + """ + data = self.data + data['created'] = 0 + + serializer = CommentSerializer(data=data) + self.assertEquals(serializer.is_valid(), False) + + self.assertIn('created', serializer.errors) + class CustomValidationTests(TestCase): class CommentSerializerWithFieldValidator(CommentSerializer):