diff options
| author | Tom Christie | 2014-12-01 10:48:45 +0000 |
|---|---|---|
| committer | Tom Christie | 2014-12-01 10:48:45 +0000 |
| commit | b9503cd603613e4ae72b7718ba70a00b1537b289 (patch) | |
| tree | 495a4302512d8e0c4e9f24e78dc0ce3e56a3dbba /rest_framework/serializers.py | |
| parent | 72c4ec4e189796e506655e275cd9c77abe98e1b9 (diff) | |
| download | django-rest-framework-b9503cd603613e4ae72b7718ba70a00b1537b289.tar.bz2 | |
Support Django's core ValidationError for backwards compat. Refs #2145.
Diffstat (limited to 'rest_framework/serializers.py')
| -rw-r--r-- | rest_framework/serializers.py | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index f7aa3a7d..de0d026d 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -11,6 +11,7 @@ python primitives. response content is handled by parsers and renderers. """ from django.core.exceptions import ImproperlyConfigured +from django.core.exceptions import ValidationError as DjangoValidationError from django.db import models from django.db.models.fields import FieldDoesNotExist from django.utils import six @@ -330,6 +331,14 @@ class Serializer(BaseSerializer): raise ValidationError({ api_settings.NON_FIELD_ERRORS_KEY: [exc.detail] }) + except DjangoValidationError as exc: + # Normally you should raise `serializers.ValidationError` + # inside your codebase, but we handle Django's validation + # exception class as well for simpler compat. + # Eg. Calling Model.clean() explictily inside Serializer.validate() + raise ValidationError({ + api_settings.NON_FIELD_ERRORS_KEY: list(exc.messages) + }) return value @@ -353,6 +362,8 @@ class Serializer(BaseSerializer): validated_value = validate_method(validated_value) except ValidationError as exc: errors[field.field_name] = exc.detail + except DjangoValidationError as exc: + errors[field.field_name] = list(exc.messages) except SkipField: pass else: |
