1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| ############### mixins.py ################ # 类中调用的方法均在 GenericAPIView 类中实现,所以下列类需要结合 GenericAPIView 使用 class ListModelMixin(object) # 查看继承类 def list(self, request, *args, **kwargs)
class CreateModelMixin(object) # 添加继承类 def create(self, request, *args, **kwargs
class RetrieveModelMixin(object) # 查看单条继承类 def retrieve(self, request, *args, **kwargs)
class UpdateModelMixin(object) # 更新单条继承类 def update(self, request, *args, **kwargs)
class DestroyModelMixin(object) # 删除单条继承类 def destroy(self, request, *args, **kwargs)
############### generics.py ############### class GenericAPIView(views.APIView) pass # 这个是懒得写,主要结合上边的使用
class ListCreateAPIView(mixins.ListModelMixin, mixins.CreateModelMixin, GenericAPIView): def get(self, request, *args, **kwargs): return self.list(request, *args, **kwargs)
def post(self, request, *args, **kwargs): return self.create(request, *args, **kwargs)
class RetrieveUpdateAPIView(mixins.RetrieveModelMixin, mixins.UpdateModelMixin, GenericAPIView): def get(self, request, *args, **kwargs): return self.retrieve(request, *args, **kwargs)
def put(self, request, *args, **kwargs): return self.update(request, *args, **kwargs)
def patch(self, request, *args, **kwargs): return self.partial_update(request, *args, **kwargs) )
############### viewsets.py ############### class ModelViewSet(mixins.CreateModelMixin, mixins.RetrieveModelMixin, mixins.UpdateModelMixin, mixins.DestroyModelMixin, mixins.ListModelMixin, GenericViewSet) pass # 这个是真pass
class GenericViewSet(ViewSetMixin, generics.GenericAPIView): pass # 这个也是真pass
class ViewSetMixin(object): def as_view(cls, actions=None, **initkwargs) def view(request, *args, **kwargs): def initialize_request(self, request, *args, **kwargs):
|