python - 'GenericApiView' 对象在我的views.py 文件中没有属性'update'
问题描述
.update 函数在我的 view.py 文件中不支持这个 genericApiView,它给了我一条错误消息 'GenericApiView' object has no attribute 'update' 。
class GenericApiView(generics.GenericAPIView,mixins.ListModelMixin,
mixins.CreateModelMixin,mixins.RetrieveModelMixin,mixins.DestroyModelMixin):
serializer_class=ArticleSerializer
queryset=Article.objects.all()
lookup_field='id'
authentication_classes=[SessionAuthentication,BaseAuthentication]
permission_classes=[IsAuthenticated]
def get(self,request, id=None):
if id:
return self.retrieve(request)
else:
return self.list(request)
def post(self,request,id):
return self.create(request,id)
# Error in this function
def put(self,request,id):
return self.update(request,id)
def delete(self,request,id=None):
return self.destroy(request,id)
解决方案
您的GenericApiView
类没有继承自mixins.UpdateModelMixin
,并且当put
方法调用时self.update()
它会导致AttributeError
. 可能的解决方案:
class GenericApiView(generics.GenericAPIView,
mixins.ListModelMixin,
mixins.CreateModelMixin,
mixins.RetrieveModelMixin,
mixins.DestroyModelMixin,
mixins.UpdateModelMixin): # added the mixin
# ...
推荐阅读
- php - “auth”从何而来
- makefile - 获取 mk 文件中的时间戳
- javascript - (reactjs) 如何设置承诺或将当前状态设置为本地存储
- linux - 如何使用 C++ 代码中的 setsockopt() 调用更改 TCP 拥塞控制算法
- angular - 我正在研究购物车,我如何在用户使用 jwt 注销后记住他的购物车状态
- c++ - 在不同循环内多次声明变量的优点
- symfony - Symfony 4 - UniqueEntity :我希望几家公司可以为一项服务使用相同的名称,但一家公司不能有两个具有相同名称的服务
- objective-c - NSTableView hideRowsAtIndexes 在 Sierra 上不起作用
- vue.js - 如何在 vue-table-2 中获取当前页码?我正在使用服务器端表
- windows - Windows xcopy 不会将文件复制回目录