5

I need to delete all objects in my table. Is possible to have a request " http://localhost:8000/api/products/delete_all/ " and when i do a get request i delete all objects. I saw this solution Delete multiple objects in django but i don't know if is possible to implement this in a moldeViewSet.

Views.py

class ProductModelViews(viewsets.ModelViewSet):
        permission_classes  =(permissions.IsAuthenticated,)
        queryset = ProductModel.objects.all()
        serializer_class = TestProductModelSerializer

1 Answer 1

7

I found the solution

class ProductModelViews(viewsets.ModelViewSet):
        permission_classes  =(permissions.IsAuthenticated,)
        queryset = ProductModel.objects.all()
        serializer_class = TestProductModelSerializer

        @action(detail=False, methods=['post'])
        def delete_all(self, request):
                Product.objects.all().delete()
                return Response('success')

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

Comments

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.