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
    该花的钱要花,该吃的饭要吃。
  • 相关阅读:
    oracle用户被锁死
    windows远程桌面智能调整大小
    批量ping测试脚本
    信息的组织和提取方法
    BeautifulSoup
    requests模块学习
    Telerik Fiddler 应用方法
    js 时间格式换成 把字符串(yyyymmdd)转换成日期格式(yyyy-mm-dd)记录
    vuedraggable 拖拽 应用 不同列表之间的拖拽
    vue项目图片上传 vant van-uploader 图片上传
  • 原文地址:https://www.cnblogs.com/chao666/p/12284407.html
Copyright © 2011-2022 走看看