0

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.

1 Answer 1

1

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']

Sign up to request clarification or add additional context in comments.

1 Comment

I try it and it works. Thanks a lot for solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.