Ejemplo n.º 1
0
    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,
        )
Ejemplo n.º 2
0
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)