def post(self, request): serializer = BookSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response( serializer.errors, status=status.HTTP_400_BAD_REQUEST, )
def book_detail(request, pk, format=None): try: book = Book.objects.get(pk=pk) except: return Response(status=status.HTTP_404_NOT_FOUND) serializer = BookSerializer(book) return Response(serializer.data)
def book_list(request, format=None): if request.method == 'GET': books = Book.objects.all() book_serializer = BookSerializer(books, many = True) return Response(book_serializer.data) elif request.method == 'POST': book_serializer = BookSerializer(data = request.data) if book_serializer.is_valid(): book_serializer.save() return Response(book_serializer.data, status = status.HTTP_201_CREATED) else: return Response(book_serializer.errors, status = status.HTTP_400_BAD_REQUEST) else: return Response(book_serializer.errors, status = status.HTTP_400_BAD_REQUEST)
def get(self, request, pk): book = Book.objects.get(pk=pk) serializer = BookSerializer(book) return Response({'books': serializer.data})
def get(self, request): books = Book.objects.all() serializer = BookSerializer(books, many=True) return Response({'books': serializer.data})
def get(self, request, pk): book = Book.objects.get(pk=pk) serializer = BookSerializer(book, many=False) return Response(serializer.data)