Procházet zdrojové kódy

added dedicated bulk methods for create, update and delete in DRF3

Miroslav Shubernetskiy před 10 roky
rodič
revize
8c6dbca11c
1 změnil soubory, kde provedl 16 přidání a 7 odebrání
  1. 16 7
      rest_framework_bulk/drf3/mixins.py

+ 16 - 7
rest_framework_bulk/drf3/mixins.py

@@ -32,9 +32,12 @@ class BulkCreateModelMixin(CreateModelMixin):
         else:
             serializer = self.get_serializer(data=request.data, many=True)
             serializer.is_valid(raise_exception=True)
-            self.perform_create(serializer)
+            self.perform_bulk_create(serializer)
             return Response(serializer.data, status=status.HTTP_201_CREATED)
 
+    def perform_bulk_create(self, serializer):
+        return self.perform_create(serializer)
+
 
 class BulkUpdateModelMixin(object):
     """
@@ -72,16 +75,19 @@ class BulkUpdateModelMixin(object):
             partial=partial,
         )
         serializer.is_valid(raise_exception=True)
-        self.perform_update(serializer)
+        self.perform_bulk_update(serializer)
         return Response(serializer.data, status=status.HTTP_200_OK)
 
-    def perform_update(self, serializer):
-        serializer.save()
-
     def partial_bulk_update(self, request, *args, **kwargs):
         kwargs['partial'] = True
         return self.bulk_update(request, *args, **kwargs)
 
+    def perform_update(self, serializer):
+        serializer.save()
+
+    def perform_bulk_update(self, serializer):
+        return self.perform_update(serializer)
+
 
 class BulkDestroyModelMixin(object):
     """
@@ -104,10 +110,13 @@ class BulkDestroyModelMixin(object):
         if not self.allow_bulk_destroy(qs, filtered):
             return Response(status=status.HTTP_400_BAD_REQUEST)
 
-        for obj in filtered:
-            self.perform_destroy(obj)
+        self.perform_bulk_destroy(filtered)
 
         return Response(status=status.HTTP_204_NO_CONTENT)
 
     def perform_destroy(self, instance):
         instance.delete()
+
+    def perform_bulk_destroy(self, objects):
+        for obj in objects:
+            self.perform_destroy(obj)