Django/DRF - 405 Method not allowed on DELETE operation

后端 未结 2 968
孤独总比滥情好
孤独总比滥情好 2020-12-06 16:24

I\'m working with two dev servers on my local machine (node & django\'s).

I\'ve added django-cors-headers to the project to allow all origins & methods (on d

相关标签:
2条回答
  • 2020-12-06 17:22

    The response looks very similar to that of the list view (/api/resource/) for a ViewSet. List views only support GET, to list all of the objects, and POST to create a new object.

    DELETE requests are only allowed on the detail view (/api/resource/1/). This is because Django REST Framework needs to know what object you are looking to delete, and this information cannot be retrieved from just the list view.

    0 讨论(0)
  • 2020-12-06 17:26

    If you need to connect http method DELETE with URL without pk in DRF try this inside of your ModelViewSet:

    @action(methods=['delete'], detail=False)
    def delete(self, request):
        # your code
    

    UPD: Note that action attribute inside of ModelViewSet class will be None due request. If you check it somewhere, handle not only action name, but request method and request path.

    0 讨论(0)
提交回复
热议问题