【DRF框架】视图组件

基于mixins视图类

from rest_framework import mixins

# 创建视图
class CreateModelMixin(object)    
    def create(self, request, *args, **kwargs):

# 查看所有视图
class ListModelMixin(object)
    def list(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视图类

from rest_framework import generics


class GenericAPIView(views.APIView):
    # 获取 queryset 和 serializer_class
    

# POST请求创建数据
class CreateAPIView(mixins.CreateModelMixin,GenericAPIView):
    def post(self, request, *args, **kwargs):
    
# GET请求查询所有数据
class ListAPIView(mixins.ListModelMixin,GenericAPIView):
    def get(self, request, *args, **kwargs):
    
# GET请求查询单条数据
class RetrieveAPIView(mixins.RetrieveModelMixin,GenericAPIView):
    def get(self, request, *args, **kwargs):

# DELETE请求删除数据
class DestroyAPIView(mixins.DestroyModelMixin,GenericAPIView):
    def delete(self, request, *args, **kwargs):

# PUT/PATCH请求更新数据
class UpdateAPIView(mixins.UpdateModelMixin,GenericAPIView):
    def put(self, request, *args, **kwargs):
    def patch(self, request, *args, **kwargs):

 

基于generics视图类(组合版)

# 新增/查询所有数据的视图类
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)

# 删除数据/单条数据查询的视图类
class RetrieveDestroyAPIView(mixins.RetrieveModelMixin,
                             mixins.DestroyModelMixin,
                             GenericAPIView):

    def get(self, request, *args, **kwargs):
        return self.retrieve(request, *args, **kwargs)

    def delete(self, request, *args, **kwargs):
        return self.destroy(request, *args, **kwargs)

# 删除数据/更新数据/单条数据查询的视图类
class RetrieveUpdateDestroyAPIView(mixins.RetrieveModelMixin,
                                   mixins.UpdateModelMixin,
                                   mixins.DestroyModelMixin,
                                   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)

    def delete(self, request, *args, **kwargs):
        return self.destroy(request, *args, **kwargs)

 

基于两个路由和两个视图类的接口

# URL路由
url(r'^book_list/$',BookView.as_view()),
url(r'^book_list/(?P<pk>\d+)/$',EditBookView.as_view()),
'''
基于DRF框架的generics视图类
'''


from rest_framework import generics
from .models import Book
from utils.model_serializers import BookSerializer

# 查询所有/新增,不带参数
class BookView(generics.ListCreateAPIView):
    queryset = Book.objects.all()
    serializer_class = BookSerializer

# 查询/更新/删除单条数据,带参数
class EditBookView(generics.RetrieveUpdateDestroyAPIView):
    queryset = Book.objects.all()
    serializer_class = BookSerializer

 

 

viewsets.ModelViewSet类

class GenericViewSet(ViewSetMixin, generics.GenericAPIView):

class ModelViewSet(mixins.CreateModelMixin,
                   mixins.RetrieveModelMixin,
                   mixins.UpdateModelMixin,
                   mixins.DestroyModelMixin,
                   mixins.ListModelMixin,
                   GenericViewSet):

 

基于两个路由和一个视图类的接口

# 继承ViewSetMixin的视图类的url才能传参,参数必须是pk
    url(r'book_list/$',BookView.as_view({'get':'list','post':'create'})),
    url(r'book_list/(?P<pk>\d+)/$',BookView.as_view({'get':'retrieve','put':'update','delete':'destroy'}))
from .models import Book
from utils.model_serializers import BookSerializer
from rest_framework import viewsets


# viewsets.ModelViewSet 封装了5个方法
class BookView(viewsets.ModelViewSet):
    queryset = Book.objects.all()
    serializer_class = BookSerializer

 

 

DRF框架自带视图组件的图解 

 

源码流程

 

# 3、处理后:
url(r'^authors/$',ViewSetMixin.view)

    def as_view(cls, actions=None, **initkwargs):
        view.actions = actions
        
        def view(request, *args, **kwargs):
            pass
            
        return csrf_exempt(view)
    

# 4、用户请求url,调用view函数,传入字典
def view(request, *args, **kwargs):
    self = cls(**initkwargs)
    self.action_map = actions
    for method, action in actions.items():  {"get":"list", "post":"create"}
        handler = getattr(self, action)   # self.list, self.create
        setattr(self, method, handler)  # self.get = self.list, self.post = self.create
    
    # getattr(self,"get")  # self.list
    # getattr(self,"post")  # self.create

    return self.dispatch(request, *args, **kwargs)


# 5、在AuthorModelView的父类中查出并调用dispatch方法,
class APIView(View):
    def dispatch(self, request, *args, **kwargs):
        # get = list. post = create
        if request.method.lower() in self.http_method_names:  # get
            handler = getattr(self, request.method.lower(), self.http_method_not_allowed)  # list
        
        response = handler(request, *args, **kwargs)
        
        return self.response  # self.list

 

posted @ 2018-12-15 16:43  st--st  阅读(228)  评论(0编辑  收藏  举报