Skip to content

Instantly share code, notes, and snippets.

@brunomichetti
Created August 21, 2023 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunomichetti/7a71062d654fff12618dfd7f55cdd60b to your computer and use it in GitHub Desktop.
Save brunomichetti/7a71062d654fff12618dfd7f55cdd60b to your computer and use it in GitHub Desktop.
...
from rest_framework.permissions import IsAuthenticated
...
# Add this to the UsersViewSet
def get_permissions(self):
if self.action == "destroy":
return [IsAuthenticated()]
return super().get_permissions()
# Add this to the UsersViewSet
def destroy(self, request, pk, *args, **kwargs):
"""
Deletes an authenticated user. Requires authentication.
Only a user can delete its own account.
"""
user = request.user
if user.id != int(pk):
raise ValidationError("Only a user can delete its own account.")
user.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment