Reputation: 202
I'm doing a CreateApiView class and this method inside class and error is : "detail": "Method \"GET\" not allowed.",
class RetractBidAPIView(ListCreateAPIView):
permission_classes = (permissions.IsAuthenticated,)
serializer_class = RetractBidSerializer
queryset = ''
def create(self, request, pk, *args, **kwargs):
auction = get_object_or_404(Auction, pk=pk)
date_now = datetime.now(timezone.utc)
serializer = self.get_serializer(data=request.data,
context={"request": request})
serializer.is_valid(raise_exception=True)
serializer.save(buyer=request.user, auction=auction)
bid = get_object_or_404(Bid, pk=current_bid.pk)
bid.delete()
return Response(serializer.data, {"detail": "You bid is retracted"}, status=status.HTTP_200_OK)
Upvotes: 2
Views: 2234
Reputation: 362
class RetractBidAPIView(APIView)
permission_classes = (permissions.IsAuthenticated,)
def get(self, request, *args, **kwargs):
auction = get_object_or_404(Auction, pk=self.kwargs.get(pk)
date_now = datetime.now(timezone.utc)
serializer = RetractBidSerializer(data=request.data,
context={"request": request})
serializer.is_valid(raise_exception=True)
serializer.save(buyer=request.user, auction=auction)
bid = get_object_or_404(Bid, pk=current_bid.pk)
bid.delete()
return Response(serializer.data, {"detail": "You bid is retracted"},
status=status.HTTP_200_OK)
Upvotes: 1
Reputation: 476739
In short: since you change entities (well you seem to delete one here), you can indeed not trigger the view with a GET request.
A GET request is supposed to have no side-effects, so that means that the entities remain the same (the same number of entities, and the same values).
A CreateApiView
[drf-doc] thus implements:
(...)
Provides a
post
method handler.
So you can only make POST requests to this view, it does not allow DELETE, GET, PATCH, PUT, etc. requests.
You can for example make a POST request with the requests
library, or with curl -X POST ...
[wiki].
That being said, your implementation looks more like a DestroyApiView
[drf-doc]. This view handles DELETE requests.
Upvotes: 4