覆盖django rest generics CreateAPIView中的创建方法
override create method in django rest generics CreateAPIView
我的views.py django 应用程序如下,
class MemberCreate(generics.CreateAPIView):
queryset = members.objects.all()
serializer_class = MemberSerializer
permission_classes = (permissions.IsAdminUser,)
def create(self, serializer):
''' I wanted to do some stuff with serializer.data here '''
pass
在上面,如果我重写创建函数,return 会失败并出现以下错误,即使我什么都不做,只是写 pass 它也会失败,
AssertionError: Expected a `Response`, `HttpResponse` or `HttpStreamingResponse` to be returned from the view, but received a `<class 'NoneType'>`
return 应该是 instanceof
Response
。您可以 return parent
class 回复,在您的 stuff
之后
def create(self, request, *args, **kwargs):
''' I wanted to do some stuff with serializer.data here '''
return super(MemberCreate, self).create(request, *args, **kwargs)
否则,如果您不想要父响应,则只需 return 一个 Response
实例
def create(self, request, *args, **kwargs):
''' I wanted to do some stuff with serializer.data here '''
return Response(status=204)
您的视图应该 return 一个 Response
对象,如您的 AssertionError
堆栈跟踪中所述。
对于你的情况,你可以尝试 return 一个空的 Response
来测试你的观点和你的 serializer.data
from rest_framework.response import Response
from rest_framework import status
class MemberCreate(generics.CreateAPIView):
queryset = members.objects.all()
serializer_class = MemberSerializer
permission_classes = (permissions.IsAdminUser,)
def create(self, request, *args, **kwargs):
serializer = self.serializer_class(...)
data = serializer.data
# ...
return Response(status=status.HTTP_204_NO_CONTENT)
通常,您希望 return 您的 serializer.validated_data
最后,所以这一行可能如下所示:
return Response(serializer.validated_data, status=status.HTTP_201_CREATED)
我的views.py django 应用程序如下,
class MemberCreate(generics.CreateAPIView):
queryset = members.objects.all()
serializer_class = MemberSerializer
permission_classes = (permissions.IsAdminUser,)
def create(self, serializer):
''' I wanted to do some stuff with serializer.data here '''
pass
在上面,如果我重写创建函数,return 会失败并出现以下错误,即使我什么都不做,只是写 pass 它也会失败,
AssertionError: Expected a `Response`, `HttpResponse` or `HttpStreamingResponse` to be returned from the view, but received a `<class 'NoneType'>`
return 应该是 instanceof
Response
。您可以 return parent
class 回复,在您的 stuff
之后
def create(self, request, *args, **kwargs):
''' I wanted to do some stuff with serializer.data here '''
return super(MemberCreate, self).create(request, *args, **kwargs)
否则,如果您不想要父响应,则只需 return 一个 Response
实例
def create(self, request, *args, **kwargs):
''' I wanted to do some stuff with serializer.data here '''
return Response(status=204)
您的视图应该 return 一个 Response
对象,如您的 AssertionError
堆栈跟踪中所述。
对于你的情况,你可以尝试 return 一个空的 Response
来测试你的观点和你的 serializer.data
from rest_framework.response import Response
from rest_framework import status
class MemberCreate(generics.CreateAPIView):
queryset = members.objects.all()
serializer_class = MemberSerializer
permission_classes = (permissions.IsAdminUser,)
def create(self, request, *args, **kwargs):
serializer = self.serializer_class(...)
data = serializer.data
# ...
return Response(status=status.HTTP_204_NO_CONTENT)
通常,您希望 return 您的 serializer.validated_data
最后,所以这一行可能如下所示:
return Response(serializer.validated_data, status=status.HTTP_201_CREATED)