这是我在Views.py中的代码
class NotificationsViewSet(viewsets.ModelViewSet):
permission_classes = [permissions.IsAuthenticated, TokenHasReadWriteScope]
queryset = Notifications.objects.all()
serializer_class = NotificationsSerializer
filter_fields = ('status','task','survey_type',)
def put(self, request, pk, format=None):
notifications = self.get_object(pk)
serializer = NotificationsSerializer(notifications, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def delete(self, request, pk, format=None):
notifications = self.get_object(pk)
notifications.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
当我尝试使用方法PUT时,我收到错误put() takes at least 3 arguments (2 given)
。我的代码出了什么问题?
答案 0 :(得分:1)
def put(self, request, pk, format=None):
最少需要3个参数。像:
self.put(request, pk)
需要传递 self
变量。
可替换地,
put(self, request, pk)
最多4个参数,
self.put(request, pk, format)
答案 1 :(得分:0)
这可能是因为你在没有定义pk的set入口点上做了PUT。将其设为可选:
def put(self, request, pk=None, format=None):
请注意,如果您覆盖put
,则无法使用viewset。