diff --git a/rest_framework/authtoken/serializers.py b/rest_framework/authtoken/serializers.py index 99ea86e86..7c49f3305 100644 --- a/rest_framework/authtoken/serializers.py +++ b/rest_framework/authtoken/serializers.py @@ -2,7 +2,6 @@ from django.contrib.auth import authenticate from django.utils.translation import ugettext_lazy as _ from rest_framework import serializers -from rest_framework.exceptions import ValidationErrorMessage class AuthTokenSerializer(serializers.Serializer): @@ -20,24 +19,19 @@ class AuthTokenSerializer(serializers.Serializer): if not user.is_active: msg = _('User account is disabled.') raise serializers.ValidationError( - ValidationErrorMessage( - msg, - code='authorization') - ) + msg, + code='authorization') else: msg = _('Unable to log in with provided credentials.') raise serializers.ValidationError( - ValidationErrorMessage( - msg, - code='authorization') - ) + msg, + code='authorization') + else: msg = _('Must include "username" and "password".') raise serializers.ValidationError( - ValidationErrorMessage( - msg, - code='authorization') - ) + msg, + code='authorization') attrs['user'] = user return attrs diff --git a/rest_framework/exceptions.py b/rest_framework/exceptions.py index 7bb12b773..7465415ea 100644 --- a/rest_framework/exceptions.py +++ b/rest_framework/exceptions.py @@ -87,7 +87,11 @@ class ValidationErrorMessage(six.text_type): class ValidationError(APIException): status_code = status.HTTP_400_BAD_REQUEST - def __init__(self, detail): + def __init__(self, detail, code=None): + # If code is there, this means we are dealing with a message. + if code and not isinstance(detail, ValidationErrorMessage): + detail = ValidationErrorMessage(detail, code=code) + # For validation errors the 'detail' key is always required. # The details should always be coerced to a list if not already. if not isinstance(detail, dict) and not isinstance(detail, list): diff --git a/rest_framework/fields.py b/rest_framework/fields.py index d3bb32629..2932de4fe 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -32,8 +32,7 @@ from rest_framework.compat import ( unicode_to_repr ) from rest_framework.exceptions import ( - ValidationError, ValidationErrorMessage, - build_error_from_django_validation_error + ValidationError, build_error_from_django_validation_error ) from rest_framework.settings import api_settings from rest_framework.utils import html, humanize_datetime, representation @@ -546,7 +545,7 @@ class Field(object): msg = MISSING_ERROR_MESSAGE.format(class_name=class_name, key=key) raise AssertionError(msg) message_string = msg.format(**kwargs) - raise ValidationError(ValidationErrorMessage(message_string, code=key)) + raise ValidationError(message_string, code=key) @cached_property def root(self): diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index db07df0a3..79f80e30a 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -24,6 +24,7 @@ from rest_framework import exceptions from rest_framework.compat import DurationField as ModelDurationField from rest_framework.compat import JSONField as ModelJSONField from rest_framework.compat import postgres_fields, unicode_to_repr +from rest_framework.exceptions import ValidationErrorMessage from rest_framework.utils import model_meta from rest_framework.utils.field_mapping import ( ClassLookupDict, get_field_kwargs, get_nested_relation_kwargs, diff --git a/rest_framework/validators.py b/rest_framework/validators.py index 86a8d75fc..11e986232 100644 --- a/rest_framework/validators.py +++ b/rest_framework/validators.py @@ -60,8 +60,7 @@ class UniqueValidator(object): queryset = self.filter_queryset(value, queryset) queryset = self.exclude_current_instance(queryset) if queryset.exists(): - raise ValidationError(ValidationErrorMessage(self.message, - code='unique')) + raise ValidationError(self.message, code='unique') def __repr__(self): return unicode_to_repr('<%s(queryset=%s)>' % ( @@ -152,10 +151,8 @@ class UniqueTogetherValidator(object): if None not in checked_values and queryset.exists(): field_names = ', '.join(self.fields) raise ValidationError( - ValidationErrorMessage( - self.message.format(field_names=field_names), - code='unique') - ) + self.message.format(field_names=field_names), + code='unique') def __repr__(self): return unicode_to_repr('<%s(queryset=%s, fields=%s)>' % (