我正在关注问题Django REST Framework how to specify error code when raising validation error in serializer的答案,以便自定义异常处理,但是我收到了错误。
这是我的自定义异常处理程序:
from rest_framework.exceptions import APIException
from django.utils.encoding import force_text
from rest_framework import status
class CustomValidation(APIException):
status_code = status.HTTP_500_INTERNAL_SERVER_ERROR
default_detail = 'A server error ocurred'
def __init__(self, detail, field, status_code):
if status_code is not None:
self.status_code = status_code
if detail is not None:
self.detail = {field: force_text(detail)}
else:
self.detail = {'detail': force_text(self.default_detail)}
这是我提出异常的地方:
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ('id', 'username', 'first_name', 'last_name', 'email', 'password')
def create(self, validated_data):
try:
user = models.User.objects.get(email=validated_data.get('email'))
except User.DoesNotExist:
user = models.User.objects.create(**validated_data)
user.set_password(user.password)
user.save()
Token.objects.create(user=user)
return user
else:
raise CustomValidation('eMail already in use', 'email', status_code=status.HTTP_409_CONFLICT)
这是我得到的追溯:
Internal Server Error: /es/users_manager/users/
Traceback (most recent call last):
File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/rest_framework/views.py", line 480, in dispatch
response = handler(request, *args, **kwargs)
File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/rest_framework/mixins.py", line 21, in create
self.perform_create(serializer)
File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/rest_framework/mixins.py", line 26, in perform_create
serializer.save()
File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/rest_framework/serializers.py", line 214, in save
self.instance = self.create(validated_data)
File "/Users/hugovillalobos/Documents/Code/IntellibookProject/Intellibook/UsersManagerApp/serializers.py", line 25, in create
raise CustomValidation('eMail already in use', 'email', status_code=status.HTTP_409_CONFLICT)
GeneralApp.utils.CustomValidation: {'email': 'eMail already in use'}
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/django/core/handlers/base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/rest_framework/viewsets.py", line 103, in view
return self.dispatch(request, *args, **kwargs)
File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/rest_framework/views.py", line 483, in dispatch
response = self.handle_exception(exc)
File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/rest_framework/views.py", line 440, in handle_exception
response = exception_handler(exc, context)
TypeError: __init__() missing 1 required positional argument: 'status_code'
[07/Jun/2018 02:42:16] "POST /es/users_manager/users/ HTTP/1.1" 500 102763
我不知道我错过了什么。