[Answered ]-Delete User from db Django Rest_framework View function

1👍

You can make use of the DestroyAPIView which allows you to perform the DELETE operation with ease.

For that, create a new view class as,

class DeleteUserAPI(generics.DestroyAPIView):
    serializer_class = UserSerializer
    authentication_classes = (authentication.TokenAuthentication,)
    permission_classes = (permissions.IsAuthenticated,)
    queryset = get_user_model().objects.all()

and wire this view in your urls.py as,

urlpatterns = [
    path('delete-user/<int:pk>/', views.DeleteUserAPI.as_view(), name='delete-user'),
    # rest of your config
    path('create/', views.CreateUserView.as_view(), name='create'),
    path('token/', views.CreateTokenView.as_view(), name='token'),
    path('me/', views.ManageUserView.as_view(), name='me'),
    path('all_users/', views.RetrieveUsersView.as_view(), name='all_users')
]

Alternatively, you can use the ModelViewSet too to delete the objects.

👤JPG

Leave a comment