栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Drf视图

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Drf视图

Django REST framework是一个建立在Django基础之上的Web 应用开发框架,可以快速的开发REST API接口应用。

  1. 尽量使用https协议

  2. 域名当中尽量出现api的字样 api.laufing.com

  3. 路径里尽量出现版本号 如: /v1/users/users/

  4. !! 路径当中尽量使用名词, 如 /v1/users/users/

  5. 使用请求方法 来表示对资源的操作; GET /v1/users/users/; POST /v1/users/users/; PUT /v1/users/users/id/; DELETe /v1/users/users/id/

  6. 路径中拼接查询字符串,实现数据的过滤、分页、排序等 /v1/users/users/?page=3&pageSize=10&orderBy=age&desc=true

    原生django: request.GET.get("page") request.POST.get("username")

    DRF: request.query_params.get("page") request.data.get("username")

1.2 混入扩展类  

DRF框架提供了5个Mixin扩展类,封装了通用的增、删、改、查(一个和多个)的代码基本流程:

扩展类封装方法说明
ListModelMixinlist封装获取一组数据的基本代码流程
CreateModelMixincreate封装新增一条数据的基本代码流程
RetrieveModelMixinretrieve封装获取指定数据的基本代码流程
UpdateModelMixinupdate封装更新指定数据的基本代码流程
DestroyModelMixindestroy封装删除指定数据的基本代码流程

子类视图类

Django框架为了方便视图的编写,还提供了9个子类视图类。

ListAPIViewGenericAPIView、ListModelMixin提供 get 方法
CreateAPIViewGenericAPIView、CreateModelMixin提供 post 方法
RetrieveAPIViewGenericAPIView、RetrieveModelMixin提供 get 方法
子类视图类继承请求处理方法
UpdateAPIViewGenericAPIView、UpdateModelMixin提供 put 方法
DestroyAPIViewGenericAPIView、DestroyModelMixin提供 delete 方法
ListCreateAPIViewGenericAPIView、ListModelMixin、CreateModelMixin提供 get 和 post 方法
RetrieveUpdateAPIViewGenericAPIView、RetrieveModelMixin、UpdateModelMixin提供 get、put 方法
RetrieveDestroyAPIViewGenericAPIView、RetrieveModelMixin、DestroyModelMixin提供 get 和 delete 方法
RetrieveUpdateDestroyAPIViewGenericAPIView、RetrieveModelMixin、UpdateModelMixin、DestroyModelMixin提供 get、put、delete方法

注:子类视图都在from rest_framework.generics模块中。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/976803.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号