gpt4 book ai didi

python - 覆盖 UpdateAPIView 的更新方法

转载 作者:太空宇宙 更新时间:2023-11-03 13:41:31 25 4
gpt4 key购买 nike

我需要在调用 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 View 一样,ViewSet 上的 update 方法应该返回一个响应。现在你没有返回任何东西,这就是 Django 提示接收 NoneType(因为这是默认返回值)的原因。

问题出在您的 update 方法的最后一行,您在其中调用父级 update 但没有返回它。

super(CarView, self).update(request, *args, **kwargs)

如果您返回它,来自通常定义的 update 方法的响应将沿着链向下传递并按您预期的方式呈现。

return super(CarView, self).update(request, *args, **kwargs)

关于python - 覆盖 UpdateAPIView 的更新方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30325538/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com