2022-10-23

2022-10-22  本文已影响0人  你猜猜我是谁or宇宙第一帅
 class ProjectDetailApiView(APIView):
    """查询单个项目、删除项目、修改项目接口"""
    def get_object(self, pk):
        try:
            return Project.objects.get(pk=pk)
        except Project.DoesNotExist:
            raise Http404

    def get(self, request, pk, format=None):
        project = self.get_object(pk)
        serializer = ProjectSerializer(project)
        return Response(response_data_format(1, "success", serializer.data), status=status.HTTP_200_OK)

    def put(self, request, pk, format=None):
        project = self.get_object(pk)
        serializer = ProjectSerializer(instance=project, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(response_data_format(1, "success", serializer.data), status=status.HTTP_200_OK)
        return Response(response_data_format(0, "fail", serializer.data), status=status.HTTP_200_OK)

    def delete(self, request, pk, format=None):
        project = self.get_object(pk)
        project.delete()
        return Response(response_data_format(0, "fail", None), status=status.HTTP_204_NO_CONTENT)


url(r'^projects/$', views.ProjectApiView.as_view()),
url(r'^projects/(?P<pk>[0-9]+)$', views.ProjectDetailApiView.as_view()),
上一篇下一篇

猜你喜欢

热点阅读