zoukankan      html  css  js  c++  java
  • DRF框架之视图子类简介

    所谓,视图子类就是继承自视图扩展类和GenericAPIView类的类。

    他们,帮我们将请求方法封装好了,我们只需要,使用视图继承这些子类即可使用其中的方法。

    1) CreateAPIView

    提供 post 方法

    继承自: GenericAPIView、CreateModelMixin

    2)ListAPIView

    提供 get 方法

    继承自:ListModelMixin、GenericAPIView

    3)RetrieveAPIView

    提供 get 方法

    继承自: RetrieveModelMixin、GenericAPIView

    4)DestoryAPIView

    提供 delete 方法

    继承自:DestoryModelMixin、GenericAPIView

    5)UpdateAPIView

    提供 put 和 patch 方法

    继承自:UpdateModelMixin、GenericAPIView

    6)RetrieveUpdateAPIView

    提供 get、put、patch方法

    继承自: RetrieveModelMixin、UpdateModelMixin、GenericAPIView

    7)RetrieveUpdateDestoryAPIView

    提供 get、put、patch、delete方法

    继承自:RetrieveModelMixin、UpdateModelMixin、DestoryModelMixin、GenericAPIView

    案例代码:

    # url(r'^books/(?P<pk>d+)/$', views.BookDetailView.as_view()),
    class BookDetailView(RetrieveAPIView, UpdateAPIView, DestroyAPIView):
        """查询、修改、删除指定图书信息"""
    
        # 指定查询集
        queryset = BookInfo.objects.all()
        # 指定序列化器
        serializer_class = BookInfoModelSerializer
    
    
    # url(r'^books/$', views.BookListView.as_view()),
    class BookListView(ListAPIView, CreateAPIView):
        """查询所有图书信息、新增图书信息"""
    
        # 指定查询集
        queryset = BookInfo.objects.all()
        # 指定序列化器
        serializer_class = BookInfoModelSerializer
    该花的钱要花,该吃的饭要吃。
  • 相关阅读:
    ASP.NET编程的十大技巧
    C#学习心得(转)
    POJ 1177 Picture (线段树)
    POJ 3067 Japan (树状数组)
    POJ 2828 Buy Tickets (线段树)
    POJ 1195 Mobile phones (二维树状数组)
    HDU 4235 Flowers (线段树)
    POJ 2886 Who Gets the Most Candies? (线段树)
    POJ 2418 Cows (树状数组)
    HDU 4339 Query (线段树)
  • 原文地址:https://www.cnblogs.com/chao666/p/12284407.html
Copyright © 2011-2022 走看看