覆盖 UpdateAPIView 的更新方法
Override update method of UpdateAPIView
我需要在调用 update() 之前执行一些操作。
我的代码
class CarView(generics.UpdateAPIView):
permission_classes = (IsAdminUser,)
serializer_class = CarSerializer
def get_queryset(self):
return ...
def update(self, request, *args, **kwargs):
# some actions
super(CarView, self).update(request, *args, **kwargs)
但是我遇到了一个错误
错误信息
Expected a Response
, HttpResponse
or HttpStreamingResponse
to be
returned from the view, but received a <type 'NoneType'>
我该如何解决?
像大多数 Django 视图一样,ViewSet
上的 update
方法应该是 return 响应。现在你没有 returning 任何东西,这就是 Django 抱怨收到 NoneType
的原因(因为这是默认的 return 值)。
问题出在您的 update
方法的最后一行,您在其中调用父 update
但没有 return 调用它。
super(CarView, self).update(request, *args, **kwargs)
如果您 return 编辑它,来自通常定义的 update
方法的响应将沿着链传递并按您预期的方式呈现。
return super(CarView, self).update(request, *args, **kwargs)
发生这种情况是因为您没有return在您的update
方法中编辑任何东西。 Django 视图期望 Response
对象被 returned。只需在 update
方法中添加一个 return
。
class CarView(generics.UpdateAPIView):
permission_classes = (IsAdminUser,)
serializer_class = CarSerializer
def get_queryset(self):
return ...
def update(self, request, *args, **kwargs):
# some actions
return super(CarView, self).update(request, *args, **kwargs)
根据文档,
REST framework supports HTTP content negotiation by providing a
Response class which allows you to return content that can be rendered
into multiple content types, depending on the client request.
The Response class subclasses Django's SimpleTemplateResponse.
Response objects are initialised with data, which should consist of
native Python primitives. REST framework then uses standard HTTP
content negotiation to determine how it should render the final
response content.
因此,要将数据呈现为不同的内容类型,您必须 return 响应。
我需要在调用 update() 之前执行一些操作。
我的代码
class CarView(generics.UpdateAPIView):
permission_classes = (IsAdminUser,)
serializer_class = CarSerializer
def get_queryset(self):
return ...
def update(self, request, *args, **kwargs):
# some actions
super(CarView, self).update(request, *args, **kwargs)
但是我遇到了一个错误
错误信息
Expected a
Response
,HttpResponse
orHttpStreamingResponse
to be returned from the view, but received a<type 'NoneType'>
我该如何解决?
像大多数 Django 视图一样,ViewSet
上的 update
方法应该是 return 响应。现在你没有 returning 任何东西,这就是 Django 抱怨收到 NoneType
的原因(因为这是默认的 return 值)。
问题出在您的 update
方法的最后一行,您在其中调用父 update
但没有 return 调用它。
super(CarView, self).update(request, *args, **kwargs)
如果您 return 编辑它,来自通常定义的 update
方法的响应将沿着链传递并按您预期的方式呈现。
return super(CarView, self).update(request, *args, **kwargs)
发生这种情况是因为您没有return在您的update
方法中编辑任何东西。 Django 视图期望 Response
对象被 returned。只需在 update
方法中添加一个 return
。
class CarView(generics.UpdateAPIView):
permission_classes = (IsAdminUser,)
serializer_class = CarSerializer
def get_queryset(self):
return ...
def update(self, request, *args, **kwargs):
# some actions
return super(CarView, self).update(request, *args, **kwargs)
根据文档,
REST framework supports HTTP content negotiation by providing a Response class which allows you to return content that can be rendered into multiple content types, depending on the client request.
The Response class subclasses Django's SimpleTemplateResponse. Response objects are initialised with data, which should consist of native Python primitives. REST framework then uses standard HTTP content negotiation to determine how it should render the final response content.
因此,要将数据呈现为不同的内容类型,您必须 return 响应。