简体   繁体   中英

Delete multiple objects in django rest framework

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM