目录
请求与响应
请求Request
1.request:drf中的request不再是django默认的HttpRequest对象而是res_framework提供的Request类的对象
2.属性:
data:解析之后的文件及非文件数据
包含POST、PUT、PATCH请求方式解析的数据
支持Json数据
query_params:与django的GET请求方式一致
3.drf默认请求格式:在res_framework.settings中可以查询
DEFAULTS = {
'DEFAULT_PARSER_CLASSES': [
'rest_framework.parsers.JSONParser',
'rest_framework.parsers.FormParser',
'rest_framework.parsers.MultiPartParser'
4.配置响应格式
全局配置:项目配置文件
局部配置:视图类中
class BookView(APIView):
parser_classes = [JSONParser,FormParser,MultiPartParser]
5.解析的使用顺序:视图类--项目配置文件--内置配置文件
6.实际项目配置:基本上都运行JSONParser,FormParser
上传文件只允许MultiPartParser
响应Response
1.Response:REST framework提供的响应类 会将响应内容转换(rander)显示成符合前端需要的类型
根据请求头中的(Accept)接收数据响应来做转换 如果没有 采用默认方式
2.drf默认解析响应编码:在res_framework.settings中可以查询
REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES': [ # 默认响应渲染类
'rest_framework.renderers.JSONRenderer', # json渲染器
'rest_framework.renderers.TemplateHTMLRenderer', # 浏览器API渲染器
],
3.配置响应格式
全局配置:项目配置文件中修改
DEFAULTS = {
# Base API policies
'DEFAULT_RENDERER_CLASSES': [
'rest_framework.renderers.JSONRenderer', # json格式
'rest_framework.renderers.BrowsableAPIRenderer', # 浏览器看到的格式
],
局部配置:视图类中
class BookView(APIView):
renderer_classes = [JSONRenderer,]
运行顺序:局部---全局---默认
4.属性:
data:为响应准备的序列化处理后的数据
status:状态码 默认200 常量(可以直接导入) (status_code:状态码的数字)
1xx---信息告知
2xx---成功
3xx---重定向
4xx---客户端错误
5xx---服务器错误
headers:存放响应头的字典
content_type:响应数据类型 可以设置参数 (content经过rander处理后的响应数据)
template_name:模板的名称
5.响应头中添加数据
5.1 原生django中向响应头中添加数据:
class User(views.View):
def get(self, request):
b = {'name': 'lili'}
res = JsonResponse(b)
# res['age'] = 19
res['name'] = 'haha'
return res
5.2 drf中向响应头中添加数据:通过Response的属性headers={'name': 'lili'}添加
class BookViews(APIView):
def get(self, request):
book_list = models.Book.objects.all()
ser = serializer.BookSerializer(instance=book_list, many=True)
return Response(ser.data, headers={'name': 'lili'})
6.解析的使用顺序:视图类--项目配置文件--内置配置文件
7.实际编码中响应一般是默认
视图组件
视图的继承
1.APIView:drf提供的顶层的视图类 通过继承写视图类 它继承django的View
2个视图基类
1.APIView:
2.GenericAPIView:继承了APIView
类属性:queryset = models.Book.objects.all()
serializer_class = serializer.BookSerializer
方法: self.get_object() # 根据pk获取单个数据
self.get_serializer # 获取要使用的序列化类
self.get_queryset() # 获取所有要序列化数据
1.基于APIView写5个接口
1.查询所有
class BookViews(APIView):
def get(self, request):
book_list = models.Book.objects.all()
ser = serializer.BookSerializer(instance=book_list, many=True)
return Response(ser.data, headers={'name': 'lili'})
2.新增
class BookViews(APIView):
def post(self, request):
create_book = serializer.BookSerializer(data=request.data)
if create_book.is_valid():
create_book.save()
return Response({'code': 100, 'msg': '新增成功'})
else:
return Response({'code': 101, 'msg': create_book.errors})
3.查询单条
class BookView(APIView):
def get(self, request, pk):
book = models.Book.objects.filter(pk=pk).first()
ser = serializer.BookSerializer(instance=book)
return Response(ser.data)
4.修改
class BookView(APIView):
def put(self, request, pk):
update_book = models.Book.objects.filter(pk=pk).first()
# 不加first是列表对象没有该AttributeError: 'QuerySet' object has no attribute '_meta' 单独的对象才有
ser = serializer.BookSerializer(instance=update_book, data=request.data)
if ser.is_valid():
ser.save()
return Response(ser.data)
else:
return Response(ser.errors)
5.删除
class BookView(APIView):
def delete(self, request, pk):
models.Book.objects.filter(pk=pk).delete()
return Response({})
2.基于GenericAPIView写5个接口
1.查询所有
from rest_framework.generics import GenericAPIView
class BookView(GenericAPIView):
queryset = models.Book.objects.all()
serializer_class = serializer.BookSerializer
def get(self, request):
book_list = self.get_queryset()
ser = self.get_serializer(instance=book_list, many=True)
return Response(ser.data)
2.新增
class BookView(GenericAPIView):
queryset = models.Book.objects.all()
serializer_class = serializer.BookSerializer
def post(self, request):
ser = self.get_serializer(data=request.data)
if ser.is_valid():
ser.save()
return Response({'code': 100, 'mag': '添加成功'})
else:
return Response({'code': 101, 'msg': ser.errors})
3.查询单条
class BookDetailView(GenericAPIView):
queryset = models.Book.objects.all()
serializer_class = serializer.BookSerializer
def get(self, request, pk):
book = self.get_object()
ser = self.get_serializer(instance=book)
return Response(ser.data)
4.修改
class BookDetailView(GenericAPIView):
queryset = models.Book.objects.all()
serializer_class = serializer.BookSerializer
def put(self, request, pk):
book = self.get_object()
ser = self.get_serializer(instance=book, data=request.data)
if ser.is_valid():
ser.save()
return Response(ser.data)
else:
return Response(ser.errors)
5.删除:self.get_queryset()获取所有序列化对象删选出删除数据删除
class BookDetailView(GenericAPIView):
queryset = models.Book.objects.all()
serializer_class = serializer.BookSerializer
def delete(self, request, pk):
self.get_queryset().filter(pk=pk).delete()
return Response({})
drf内容框架
1.前后端开发模式
2.API接口
3.postman使用
4.序列化与反序列化
5.restful规范
6.drf快速实现符合restful规范的接口
7.cbv的执行流程
8.序列化类
9.请求与响应
10.视图层
11.路由的使用
12.认证、频率、权限
13.过滤、排序、分页
14.全局异常处理
15.接口文档
16.前后端分离:jwt认证
17.RBAC:公司内部使用
18.django:admin美化
作业
1.使用GenericAPIView写出book的5个接口
1.查询所有
from rest_framework.generics import GenericAPIView
class BookView(GenericAPIView):
queryset = models.Book.objects.all()
serializer_class = serializer.BookSerializer
def get(self, request):
book_list = self.get_queryset()
ser = self.get_serializer(instance=book_list, many=True)
return Response(ser.data)
2.新增
class BookView(GenericAPIView):
queryset = models.Book.objects.all()
serializer_class = serializer.BookSerializer
def post(self, request):
ser = self.get_serializer(data=request.data)
if ser.is_valid():
ser.save()
return Response({'code': 100, 'mag': '添加成功'})
else:
return Response({'code': 101, 'msg': ser.errors})
3.查询单条
class BookDetailView(GenericAPIView):
queryset = models.Book.objects.all()
serializer_class = serializer.BookSerializer
def get(self, request, pk):
book = self.get_object()
ser = self.get_serializer(instance=book)
return Response(ser.data)
4.修改
class BookDetailView(GenericAPIView):
queryset = models.Book.objects.all()
serializer_class = serializer.BookSerializer
def put(self, request, pk):
book = self.get_object()
ser = self.get_serializer(instance=book, data=request.data)
if ser.is_valid():
ser.save()
return Response({'code':'修改成功', 'msg':ser.data})
else:
return Response({'code':'修改成功', 'msg':ser.errors})
5.删除:self.get_queryset()获取所有序列化对象删选出删除数据删除
class BookDetailView(GenericAPIView):
queryset = models.Book.objects.all()
serializer_class = serializer.BookSerializer
def delete(self, request, pk):
self.get_queryset().filter(pk=pk).delete()
return Response({})
2.使用面向对象,写5个父类, 继承GenericAPIView+某几个父类后,就有某几个接口
一、5个父类
1.查询所有
class CheckAll(object):
def get(self, request):
book_list = self.get_queryset()
ser = self.get_serializer(instance=book_list, many=True)
return Response(ser.data)
2.新增
class Create(object):
def post(self, request):
ser = self.get_serializer(data=request.data)
if ser.is_valid():
ser.save()
return Response({'code': 100, 'mag': '添加成功'})
else:
return Response({'code': 101, 'msg': ser.errors})
3.查询一条
class CheckOne(object):
def get(self, request, pk):
book = self.get_object()
ser = self.get_serializer(instance=book)
return Response(ser.data)
4.修改
class UpDate(object):
def put(self, request, pk):
book = self.get_object()
ser = self.get_serializer(instance=book, data=request.data)
if ser.is_valid():
ser.save()
return Response(ser.data)
else:
return Response(ser.errors)
5.删除
class Delete(object):
def delete(self, request, pk):
self.get_queryset().filter(pk=pk).delete()
return Response({})
二、视图类
查询所有、新增
class BookView(GenericAPIView, CheckAll, Create):
queryset = models.Book.objects.all()
serializer_class = serializer.BookSerializer
查询单个、修改、删除
class BookDetailView(GenericAPIView, CheckOne, UpDate, Delete):
queryset = models.Book.objects.all()
serializer_class = serializer.BookSerializer
3.9个视图子类
1.查询所有
class BookAll(GenericAPIView, CheckAll):
queryset = models.Book.objects.all()
serializer_class = serializer.BookSerializer
2.新增
class BookCreate(GenericAPIView, Create):
queryset = models.Book.objects.all()
serializer_class = serializer.BookSerializer
3.查询一条
class BookOne(GenericAPIView, CheckOne):
queryset = models.Book.objects.all()
serializer_class = serializer.BookSerializer
4.修改
class BookUpDate(GenericAPIView, UpDate):
queryset = models.Book.objects.all()
serializer_class = serializer.BookSerializer
5.删除
class BookDetele(GenericAPIView, Delete):
queryset = models.Book.objects.all()
serializer_class = serializer.BookSerializer
6.查询所有、新增
class BookAllCreate(GenericAPIView, CheckAll, Create):
queryset = models.Book.objects.all()
serializer_class = serializer.BookSerializer
7.查询一条、修改一条
class BookOneUpDate(GenericAPIView, CheckOne,UpDate):
queryset = models.Book.objects.all()
serializer_class = serializer.BookSerializer
8.查询一条、删除一条
class BookOneDelete(GenericAPIView, CheckOne,Delete):
queryset = models.Book.objects.all()
serializer_class = serializer.BookSerializer
9.修改一条、删除一条
class BookUpDateDelete(GenericAPIView, UpDate,Delete):
queryset = models.Book.objects.all()
serializer_class = serializer.BookSerializer
标签:ser,04,get,self,Response,class,serializer,drf
From: https://www.cnblogs.com/040714zq/p/16743241.html