如何更改DRF中的验证错误响应?

Mat*_*tko 12 python django django-rest-framework

我想更改JSON,当发生验证错误时,rest_framework或django会返回.

我将使用我的一个视图作为示例,但我想更改所有视图的错误消息.所以,我想这个观点意味着登录用户,提供电子邮件和密码.如果这些是正确的,则返回access_token.

如果我只发布密码,则返回错误400:

{"email": ["This field is required."]}
Run Code Online (Sandbox Code Playgroud)

如果密码和电子邮件不匹配:

{"detail": ["Unable to log in with provided credentials."]}
Run Code Online (Sandbox Code Playgroud)

我想要的更像是:

{"errors": [{"field": "email", "message": "This field is required."}]}

{"errors": [{"non-field-error": "Unable to log in with provided credentials."}]}
Run Code Online (Sandbox Code Playgroud)

现在这是我的观点:

class OurLoginObtainAuthToken(APIView):
    permission_classes = (AllowAny,)
    serializer_class = serializers.AuthTokenSerializer
    model = Token

    def post(self, request):
        serializer = self.serializer_class(data=request.DATA)
        if serializer.is_valid():
            #some magic
            return Response(token)           
        return Response(serializers.errors, status=status.HTTP_400_BAD_REQUEST)
Run Code Online (Sandbox Code Playgroud)

我可以访问serializer.errors并更改它们,但看起来只能以这种方式访问​​字段错误,如何更改在序列化程序验证方法中创建的验证错误?

这是我的序列化程序(它与rest_framework.authtoken.serializers.AuthTokenSerializer是相同的序列化程序)但已编辑,因此身份验证不需要用户名但电子邮件:

class AuthTokenSerializer(serializers.Serializer):
    email = serializers.CharField()
    password = serializers.CharField()

    def validate(self, attrs):
        email = attrs.get('email')
        password = attrs.get('password')
        #print email
        #print password
        if email and password:
            user = authenticate(email=email, password=password)

            if user:
                if not user.is_active:
                    msg = _('User account is disabled.')
                    raise ValidationError(msg)
                attrs['user'] = user
                return attrs
            else:
                msg = _('Unable to log in with provided credentials.')
                raise ValidationError(msg)
        else:
            msg = _('Must include "username" and "password"')
            raise ValidationError(msg)
Run Code Online (Sandbox Code Playgroud)

或者可能有一种完全不同的方法?我会非常感谢任何想法.

Tom*_*tie 11

通过应用程序中的所有视图更改错误样式的最简单方法是始终使用serializer.is_valid(raise_exception=True),然后实现定义如何创建错误响应的自定义异常处理程序.


Abd*_*rbi 10

处理错误时 DRF 的默认结构是这样的:

{"email": ["This field is required."]}
Run Code Online (Sandbox Code Playgroud)

您可以通过编写自定义异常处理程序来根据需要更改此结构。

现在假设您要实现以下结构:

{"errors": [{"field": "email", "message": "This field is required."}]}
Run Code Online (Sandbox Code Playgroud)

您的自定义异常处理程序可能是这样的:

from rest_framework.views import exception_handler


def custom_exception_handler(exc, context):
    # Call REST framework's default exception handler first,
    # to get the standard error response.
    response = exception_handler(exc, context)

    # Update the structure of the response data.
    if response is not None:
        customized_response = {}
        customized_response['errors'] = []

        for key, value in response.data.items():
            error = {'field': key, 'message': value}
            customized_response['errors'].append(error)

        response.data = customized_response

    return response
Run Code Online (Sandbox Code Playgroud)