当前位置:网站首页>知识点回顾
知识点回顾
2022-07-23 05:38:00 【დ᭄ꦿ阿楊꧔ꦿ℘⸙451】
知识点回顾
1.什么是序列化
将模型类层数据转换成json数据类型
2.反序列化
将json数据格式转换成模型类层数据格式
普通序列化器
1.既要在视图中写又要在序列化器文件中写
模型序列化器
1.只需要在视图中写
外键序列化器
1.根据外键进行序列化(默认)
2.根据描述信息进行序列化
3.根据自己定义的字段进行序列化
扩展类
准备工作
1.创建工程
2.配置文件
3.写模型类
4.生成迁移文件
5.执行迁移文件
6.写视图
GenericAPIView
特点:
1.提供了查询集和序列化器
和mixin搭配使用
mixin:提供了操作方法
mixin
ListModelMixin
列表视图扩展类,提供***list(request,
*args,**kwargs)***方法快速实现列表视图默认返回200状态码
该Mixin的 list 方法会对数据进行过滤和分页
CreateModelMixin
创建视图扩展类,提供***create(request,
*args,**kwargs)***方法快速实现创建资源的视图成功返回201状态码。
如果序列化器对前端发送的数据验证失败,返回 400错误
RetrieveModelMixin
详情视图扩展类,提供***retrieve(request,
*args,**kwargs)***方法进行单独数据的返回如果详情数据存在,返回200, 否则返回404
UpdateModelMixin
更新视图扩展类,提供***update(request,
*args,**kwargs)***方法同时提供***partial_update(request,
*args,**kwargs)***方法,可以实现局部更新。成功返回200,序列化器校验数据失败时,返回400错误
DestroyModelMixin
删除视图扩展类,提供***destroy(request, *args, **kwargs)***方法
可以快速实现删除一个存在的数据对象
成功返回204,不存在返回404
3. 扩展类
.1. CreateAPIView
- 提供post方法,可以创建一条数据
2. ListAPIView
- 提供get方法,可以获取多条数据
3. RetireveAPIView
- 提供get方法,获取某个具体数据的详情
4. DestoryAPIView
- 提供 delete 方法,可以删除某条存在数据
5. UpdateAPIView
- 提供 put 和 patch 方法,可以更新或者局部更新某条数据
6.ListCreateAPIView
- 提供 post 和 get 方法,可以创建一条数据,或获取列表数据
7. RetrieveUpdateAPIView
- 提供 get、put、patch 方法,可以获取一条数据详情,也可以更新一条数据
8. RetrieveDestroyAPIView
- 提供 get 和 delete 方法,可以获取和删除一条已存在数据
9. RetrieveUpdateDestoryAPIView
- 提供 get、put、patch、delete 方法,能实现单个数据的获取 修改 删除 更新
只要涉及到跨表的数据操作, 最好使用普通序列化器以及GenericApiView来自己实现, 不跨表操作,可以使用mixin的方法和扩展类的方法
视图类
from django.shortcuts import render
from rest_framework.generics import GenericAPIView
from .models import Cate
from .serializers import CateModelSerializer
from rest_framework.response import Response
from rest_framework.mixins import ListModelMixin,CreateModelMixin,RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin
# 查询全部信息
# class CateGenericAPIView(GenericAPIView):
# # 1.指明当前视图类操作的查询集 模型类.objects.all() queryset查询及
# queryset = Cate.objects.all()
# # 2.指明视图类需要操作的序列化器
# serializer_class=CateModelSerializer
# # 查询数据
# def get(self,request):
# try:
# # 获取查询集,此时直接调用当前视图类提供的查询集即可
# cates = self.get_queryset()
# except Exception as e:
# print(e)
# return Response(
# {"msg":"NOT FOUND"},status=404
# )
# # 创建序列化对象 对象名 = 序列化器()
# ser = self.serializer_class(cates,many = True)
# # 返回结果
# return Response(ser.data)
# 查询全部数据
# 显示全部数据和添加数据
class CateMixinView(GenericAPIView,ListModelMixin,CreateModelMixin):
# 1.指明当前操作的额查询集
queryset = Cate.objects.all()
# 2.指明当前操作需要的序列化器
serializer_class = CateModelSerializer
# ListModelMixin :封装了list方法,实现了获取全部数据
def get(self,request):
return self.list(request)
# CreateModelMixin :封装了create方法 添加逻辑包括验证都已经写好了
def post(self,request):
return self.create(request)
# 查询指定数据和修改 删除
class CateDetailMixinView(GenericAPIView,RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin):
# 1.指明当前操作需要的查询集
queryset = Cate.objects.all()
# 2.指明操作的序列化器
serializer_class = CateModelSerializer
# 查询指定数据
def get(self,request,pk):
# RetrieveModelMixin:提供retrieve来查询单个数据
return self.retrieve(request,id=pk)
# 修改数据
def put(self,request,pk):
# UpdateModelMixin:提供了update方法来进行修改的操作 ,包括逻辑验证都已经写好了
return self.update(request,id=pk)
# 删除数据
def delete(self,request,pk):
# DestroyModelMixin:提供了destroy方法来进行删除的操作
return self.destroy(request,id=pk)
边栏推荐
猜你喜欢

IO should know and should know

Flask蓝图

疫情时期加中年危机——游荡在十字街口的三个月

讲师征集令 | Apache DolphinScheduler Meetup分享嘉宾,期待你的议题和声音!

Epidemic period plus midlife crisis - three months wandering at the crossroads

超级简单的人脸识别api 只需几行代码就可以实现人脸识别

web调用接口上传图片到七牛云

Matlab中的滤波器

C language n battle -- linked list (IX)

3DMAX first skin brush weights, then attach merge
随机推荐
Database topics necessary for interview
img标签设置height和width无效
SPR:SUPERVISED PERSONALIZED RANKING BASED ON PRIOR KNOWLEDGE FOR RECOMMENDATION
Briefly describe the features and application scenarios of redis
FFmpeg 音频编码
9. Ray tracing
7、纹理映射
Powerbi Getting Started Guide
Mysql事务回滚机制与原理
Updated again, idea 2022.2 officially released
Dynamic memory management
Epidemic period plus midlife crisis - three months wandering at the crossroads
Concepts and differences of bit, bit, byte and word
Activiti工作流使用之Activiti-app的安装及流程创建
Flask源码剖析(一)请求入口
开发必备之Idea使用
Basic concepts of software testing
一次 MySQL 误操作导致的事故,「高可用」都不好使了
Master slave synchronization step read / write separation + self encountered error sharing
Redis source code and design analysis -- 13. Ordered collection objects