当前位置:网站首页>视图的使用
视图的使用
2022-07-23 05:38:00 【ZXY_lucky】
视图简介
APIView —> GenericAPIView —> 视图扩展类 —> 子类视图
1.APIView
使用前需导入
from rest_framework.views import APIView
class StaffDetaView(APIView):
def get(self,request,pk):
return Response({
"code":200,"msg":'获取成功'})
def put(self,request,pk,**kwargs):
# 根据id查找相应信息,返回对象
staff = Staff.objects.filter(id=pk).first()
if not staff:
return Response({
"code":400,
"msg":'修改数据不存在'
})
# 反序列化 更新
# partial 实现局部更新
ser = StaffSerializer(instance=staff, data=request.data, partial=True)
# 校验数据
if ser.is_valid():
ser.save() # 调用update
return Response({
"code": 200,
"msg": '修改成功'
})
else:
return Response({
"code":400,"msg":ser.errors})
return self.update(request,pk,**kwargs)
def delete(self,request,pk):
return Response({
"code":200,"msg":'删除员工成功'})
2.GenericAPIView
继承自APIView
from rest_framework.generics import GenericAPIView
通用属性
- queryset 视图查询集
- serializer_class 视图使用的序列化器
列表视图使用:
- pagination_class分页控制类
- filter_backends过滤控制后端,对字段过滤
详情页视图使用
- lookup_field 查询单一数据对象时使用的条件字段,默认为’pk’
- lookup_url_kwarg 查询单一数据时URL中的关键字参数,默认与look_field相同
通用方法:
- get_queryset()
- 返回视图使用的查询集
- 默认返回queryset属性,支持重写 - get_serializer_class()
- 返回序列化器类,默认返回serializer_class,可以重写 - get_serializer(self, *args, **kwargs)
-返回序列化器对象
详情页独有方法
- get_object()
-默认参数来过滤
class StaffView(GenericAPIView):
queryset = Staff.objects.all()
serializer_class = StaffSerializer
def get(self,request,*args,**kwargs):
staff_info = self.get_queryset()
# 序列化,返回一个序列化对象
staff_all = self.get_seializer(staff_info,many=True)
return Response({
"staff":staff_all.data
})
3.扩展类
| 扩展类 | 封装方法 | 说明 |
|---|---|---|
| ListModelMixin | List | 获取一组数据 |
| CreateModelMixin | create | 新增一条数据 |
| RetrieveModelMixin | retrieve | 获取指定数据 |
| UpdateModelMixin | update | 更新指定数据 |
| DestroyModelMixin | destroy | 删除指定数据 |
class StaffView(GenericAPIView,ListModelMixin):
queryset = Staff.objects.all()
serializer_class = StaffSerializer
def get(self,request,*args,**kwargs):
return self.list(request,*args,**kwargs)
4.视图子类
| 子类视图类 | 继承 | 请求处理方法 |
|---|---|---|
| ListAPIView | GenericAPIView、ListModelMixin | get |
| CreateAPIView | GenericAPIView、CreateModelMixin | post |
| RetrieveAPIView | GenericAPIView、RetrieveModelMixin | get |
| UpdateAPIView | GenericAPIView、UpdateModelMixin | put |
| DestroyAPIView | GenericAPIView、DestroyModelMixin | delete |
| ListCreateAPIView | GenericAPIView、ListModelMixin、CreateModelMixin | get 和 post |
| RetrieveUpdateAPIView | GenericAPIView、RetrieveModelMixin、UpdateModelMixin | get、put |
| RetrieveDestroyAPIView | GenericAPIView、RetrieveModelMixin、DestroyModelMixin | get 和 delete |
| RetrieveUpdateDestroyAPIView | GenericAPIView、RetrieveModelMixin、UpdateModelMixin、DestroyModelMixin | get、put、delete |
导入
from rest_framework.generics import *
class StaffView(ListCreateAPIView):
queryset = Staff.objects.all()
serializer_class = StaffSerializer
注:这是示例,其他视图方法一样
边栏推荐
- H1--HDMI接口测试应用2022-07-15
- 开发必备之Idea使用
- 安装企业版pycharm以及anaconda
- 2.启动函数返回值的剖析
- cuda10.0配置pytorch1.7.0+monai0.9.0
- C1 -- vivado configuration vs code text editor environment 2022-07-21
- 超级简单的人脸识别api 只需几行代码就可以实现人脸识别
- [swift bug] Xcode prompt error running playground: failed to prepare for communication with playground
- mysql语法(纯语法)
- Cadence (IX) 17.4 rules and spacing settings
猜你喜欢

Cadence (IX) 17.4 rules and spacing settings

使用聚类分析 构建信用卡高风险客户识别模型

安装企业版pycharm以及anaconda

The 12th Blue Bridge Cup embedded design and development project

Why does MySQL index use b+ tree?

“我最想要的六种编程语言!”

H1 -- HDMI interface test application 2022-07-15

Huck hurco industrial computer maintenance winmax CNC machine tool controller maintenance

UNITY VFX syntax error: unexpected token ‘#‘ at kernel CSMain

JDBC Learning and simple Encapsulation
随机推荐
达人专栏 | 还不会用 Apache Dolphinscheduler?大佬用时一个月写出的最全入门教程
【无标题】
52832dongle installation
讲师征集令 | Apache DolphinScheduler Meetup分享嘉宾,期待你的议题和声音!
Xssgame games (XSS learning) level1-15
Activiti工作流使用之新建bpmn文件
Deploy storageclass trample record
Meyer burger Siemens industrial computer maintenance and machine tool maintenance
Mysql-8.0.28 user operation or user permission Operation
Alibaba cloud object storage service OSS front and rear joint debugging
手风琴效果
视、音频分开的网站内容如何合并?批量下载代码又该如何编写?
adb常用命令
Flask源码剖析(一)请求入口
mysql语法(纯语法)
Briefly describe the features and application scenarios of redis
Heidelberg CP2000 circuit board maintenance printer host controller operation and maintenance precautions
请求数据获取与响应
Murata power maintenance switch server power maintenance and main functional features
9、光线追踪