Search code examples
djangodjango-rest-frameworkdjango-viewsdjango-permissions

AttributeError: 'Request' object has no attribute 'DELETE'


I am trying to add permission for deleting an object.

views.py

class DeleteView(APIView):
    permission_classes = [IsAllowedDelete]  
    def delete(self, request, id):
        obj = Mymodel.objects.get(id=id)
        obj.delete()
        return Response({"detail" : "Deleted successfully"}, status.HTTP_204_NO_CONTENT) 

urls.py

path('remove/<int:id>', vm.DeleteView.as_view(), name='delete_view'),

permissions.py

class IsAllowedDelete(permissions.BasePermission):        
    def has_permission(self, request, view):
        if request.method == "DELETE":
             print('id : ',request.DELETE["id"])
             return True
        else: 
            return False      

But I am getting the below error:-

AttributeError: 'Request' object has no attribute 'DELETE'

at below statement:-

 request.DELETE["id"]

Please help me to fix this.


Solution

  • Request objects probably don't have .DELETE, only .GET and .POST.

    If you want the id passed from the url. You can access it with view.kwargs['id']