Beispiel #1
0
    def get(self, request, *args, **kwargs):
        """
        查询单个图书  查询所有图书
        """
        book_id = kwargs.get("id")

        if book_id:
            book_obj = Book.objects.get(pk=book_id)
            book_serializer = BookModelSerializer(book_obj).data

            return Response({
                "status": 200,
                "message": "查询成功",
                "results": book_serializer
            })
        else:

            book_set = Book.objects.all()
            book_set_serializer = BookModelSerializer(book_set, many=True).data

            return Response({
                "status": 200,
                "message": "查询成功",
                "results": book_set_serializer
            })
Beispiel #2
0
    def get(self, request, *args, **kwargs):
        re = request.GET
        if re:
            id = re['id']
            print('单查', id)
            book = Books.objects.filter(id=id)[0]
            if book:
                flag = False
            else:
                return Response({'status': 500, 'message': '查新单个图书失败'})

        elif not re:
            # book = Books.objects.all()
            book = Books.objects.filter(is_delete=False)
            flag = True

        else:
            return Response({
                'status': status.HTTP_500_INTERNAL_SERVER_ERROR,
                'message': '查询失败'
            })

        book_ser = BookModelSerializer(book, many=flag).data
        return Response({
            'status': status.HTTP_200_OK,
            'message': '查询图书成功',
            'books': book_ser
        })
Beispiel #3
0
 def post(self, request, *args, **kwargs):
     request_data = request.data
     serializer = BookDeModelSerializer(data=request_data)
     # 检验是否合法
     serializer.is_valid(raise_exception=True)
     book_obj = serializer.save()
     return Response({
         "status": 201,
         "message": "创建成功",
         "results": BookModelSerializer(book_obj).data
     })
Beispiel #4
0
    def put(self, request, *args, **kwargs):
        """
                修改单个对象的全部字段
                全部字段:指的是所有参与反序列化的字段
                参数:要修改的值  要修改的图书的id
                :return: 修改后的对象
                """
        # 要修改的值
        request_data = request.data
        # 图书id
        book_id = kwargs.get("id")

        try:
            book_obj = Book.objects.get(pk=book_id)
        except Book.DoesNotExist:
            return Response({"status": 400, "message": "图书不存在"})
        # 前端传递过来的要修改的值  更新数据时需要校验
        book_serializer = BookModelSerializer(data=request_data,
                                              instance=book_obj)
        book_serializer.is_valid(raise_exception=True)

        # 经过序列序列化器校验后,保存修改的值
        # 如果调用序列化器时指定了关键字参数 instance, 则底层会调用update()完成更新
        book = book_serializer.save()

        return Response({
            "status": 200,
            "message": "修改成功",
            "results": BookModelSerializer(book).data
        })
Beispiel #5
0
    def patch(self, request, *args, **kwargs):
        """
                完成修改单个对象的某些字段
                """

        request_data = request.data
        book_id = kwargs.get("id")

        try:
            book_obj = Book.objects.get(pk=book_id)
        except Book.DoesNotExist:
            return Response({"status": 400, "message": "图书不存在"})

        # 如果要修改部分字段,只需要指定参数partial=True即可
        book_serializer = BookModelSerializer(data=request_data,
                                              instance=book_obj,
                                              partial=True)
        book_serializer.is_valid(raise_exception=True)

        book = book_serializer.save()

        return Response({
            "status": 200,
            "message": "修改成功",
            "results": BookModelSerializer(book).data
        })
Beispiel #6
0
    def post(self, request, *args, **kwargs):
        # 新增
        request_data = request.dara
        # 将参数交给反序列化进行校验
        serializer = BookDeModelSerializer(data=request_data)
        # 校验数据,失败则抛出异常
        serializer.is_valid(raise_exception=True)
        book_obj = serializer.save()

        return Response({
            "status": 201,
            "message": "put succeed",
            "results": BookModelSerializer(book_obj).data
        })
Beispiel #7
0
    def post(self, request, *args, **kwargs):
        """
        完成对象的新增
        """
        request_data = request.data

        # 将前端传递的参数交给反序列化器进行校验
        serializer = BookDeModelSerializer(data=request_data)

        # 校验数据是否合法 raise_exception: 一旦校验失败,立即抛出异常
        serializer.is_valid(raise_exception=True)
        book_obj = serializer.save()

        return Response({
            "status": 201,
            "message": "创建成功",
            "results": BookModelSerializer(book_obj).data
        })
Beispiel #8
0
    def put(self, request, *args, **kwargs):
        request_data = request.data
        book_id = kwargs.get("id")

        try:
            book_obj = Book.objects.get(pk=book_id)
        except Book.DoesNotExist:
            return Response({"status": 400, "message": "图书不存在"})

        book_serializer = BookDeModelSerializer(data=request_data,
                                                instance=book_obj)
        book_serializer.is_valid(raise_exception=True)

        # 经过序列序列化器校验后,保存修改的值
        book = book_serializer.save()

        return Response({
            "status": 200,
            "message": "修改成功",
            "results": BookModelSerializer(book).data
        })
Beispiel #9
0
    def post(self, request, *args, **kwargs):
        request_data = request.data

        if isinstance(request_data, dict):
            many = False
        elif isinstance(request_data, list):
            many = True
        else:
            return Response({"status": 400, "message": "请求参数格式有误"})
        serializer = BookDeModelSerializer(data=request_data, many=many)

        serializer.is_valid(raise_exception=True)
        book_obj = serializer.save()
        if book_obj:
            return Response({
                "status":
                201,
                "message":
                "创建成功",
                "results":
                BookModelSerializer(book_obj, many=many).data
            })
Beispiel #10
0
 def post(self, request, *args, **kwargs):
     # try:
     book_data = request.data
     print('获取新增数据', book_data)
     if isinstance(book_data, dict):
         flag = False
     elif isinstance(book_data, list):
         flag = True
     else:
         return Response({
             'status': status.HTTP_400_BAD_REQUEST,
             'message': '格式有误'
         })
     ser = BookDeModelSerializer(data=book_data, many=flag)
     print(111, ser)
     # if ser.is_valid():
     ser.is_valid(raise_exception=True)
     book = ser.save()
     print(222)
     return Response({
         'status': status.HTTP_200_OK,
         'message': '创建图书成功',
         'book': BookModelSerializer(book, many=flag).data
     })