Prechádzať zdrojové kódy

Example of BulkRouter implementation

Thomas Wajs 10 rokov pred
rodič
commit
91f6b37c8f
1 zmenil súbory, kde vykonal 33 pridanie a 0 odobranie
  1. 33 0
      README.rst

+ 33 - 0
README.rst

@@ -83,6 +83,39 @@ The above will allow to create the following queries
     # delete queryset (see notes)
     DELETE
 
+Router
+-------
+
+It's also pretty easy to define a router that handle the bulk operation ::
+
+
+	class BulkRouter(DefaultRouter):
+	    routes = SimpleRouter.routes
+	    routes[0] = Route(
+	        url=r'^{prefix}{trailing_slash}$',
+	        mapping={
+	            'get': 'list',
+	            'post': 'create',
+	            'put': 'bulk_update',
+	            'patch': 'partial_bulk_update',
+	            'delete': 'bulk_destroy'
+	        },
+	        name='{basename}-list',
+	        initkwargs={'suffix': 'List'}
+	    )
+	
+	class UserViewSet(BulkCreateModelMixin
+	                  BulkUpdateModelMixin,
+	                  BulkDestroyModelMixin,
+	                  viewsets.ModelViewSet):
+	    model = User
+	    
+	    def allow_bulk_destroy(self, qs, filtered):
+	        """Don't forget to fine-grain this method"""
+	
+	router = BulkRouter()
+	router.register(r'users', UserViewSet)
+
 Notes
 -----