Beispiel #1
0
 def get(self, request, pk):
     try:
         book = BookInfo.objects.get(id=pk)
     except BookInfo.DoesNotExist:
         return Response({'error': '查询失败'})
     ser = BookSerializers(book)
     return Response(ser.data)
Beispiel #2
0
    def retrieve(self, request, pk):
        try:
            book = BookInfo.objects.get(id=pk)
        except BookInfo.DoesNotExist:
            return Response({'error': '图书不存在'})

        ser = BookSerializers(book)
        return Response({'book': ser.data})
Beispiel #3
0
 def post(self, request):
     data_dict = request.data
     ser = BookSerializers(data=data_dict)
     ser.is_valid()
     if ser.errors:
         return Response({'error': '添加失败'})
     ser.save()
     return Response(ser.data)
Beispiel #4
0
    def create(self, request):
        data = request.data
        ser = BookSerializers(data=data)

        ser.is_valid()
        if ser.errors:
            return Response({'errors': ser.errors})
        ser.save()
        return Response(ser.data)
Beispiel #5
0
 def put(self, request, pk):
     try:
         book = BookInfo.objects.get(id=pk)
     except BookInfo.DoesNotExist:
         return Response({'error': '没有图书'})
     book_dict = request.data
     ser = BookSerializers(book, data=book_dict)
     ser.is_valid()
     if ser.errors:
         return Response({'error': '添加失败'})
     ser.save()
     return Response(ser.data)
Beispiel #6
0
 def update(self, request, pk):
     try:
         book = BookInfo.objects.get(id=pk)
     except BookInfo.DoesNotExist:
         return Response({'error': '图书不存在'})
     data = request.data
     ser = BookSerializers(book, data=data)
     ser.is_valid()
     if ser.errors:
         return Response({'errors': '更新失败'})
     ser.save()
     return Response(ser.data)
Beispiel #7
0
    def get(self, request):
        books = BookInfo.objects.all()

        ser = BookSerializers(books, many=True)

        return Response(ser.data)