I made a view that can use put, delete request using modelviewset and mapped it with url. I have clearly made it possible to request put and delete request to url, but if you send delete request to url, I return 405 error. What's wrong with my code? Here's my code.
views.py
class UpdateDeletePostView (ModelViewSet) :
serializer_class = PostSerializer
permission_classes = [IsAuthenticated, IsOwner]
queryset = Post.objects.all()
def update (self, request, *args, **kwargs) :
super().update(request, *args, **kwargs)
return Response({'success': '게시물이 수정 되었습니다.'}, status=200)
def destroy (self, request, *args, **kwargs) :
super().destroy(request, *args, **kwargs)
return Response({'success': '게시물이 삭제 되었습니다.'}, status=200)
feed\urls.py
path('post/<int:pk>', UpdateDeletePostView.as_view({'put': 'update', 'delete': 'destroy'})),
server\urls.py
path('feed/', include('feed.urls')),
and error
"detail": "method \delete\ not allowed"
as I wrote in the comment looks like you don't need a ViewSet
because you are handling just operations on a single item.
In general you can restrict the operations available for Views or ViewSet using proper mixins.
I suggest two possible approaches
Use Generic View
class UpdateDeletePostView(
UpdateModelMixin,
DeleteModelMixin,
GenericAPIView):
.....
and
urlpatterns = [
path('post/<int:pk>', UpdateDeletePostView.as_view()),
...
]
Use ViewSet and Router
class UpdateDeletePostViewSet(
UpdateModelMixin,
DeleteModelMixin,
GenericViewset):
.....
router = SimpleRouter()
router.register('feed', UpdateDeletePostViewSet)
urlpatterns = [
path('', include(router.urls)),
...
]