Django REST framework 视图和路由详解
作者:zpf666 时间:2022-10-16 00:29:32
DRF中的Request
在Django REST Framework中内置的Request类扩展了Django中的Request类,实现了很多方便的功能--如请求数据解析和认证等。
比如,区别于Django中的request从request.GET中获取URL参数,从request.POST中取某些情况下的POST数据。
在APIView中封装的request,就实现了请求数据的解析:
对于GET请求的参数我们通过request.query_params来获取。
对于POST请求、PUT请求的数据我们通过request.data来获取。
前提
序列化
from rest_framework import serializers
from app01 import models
class CommentSerializer(serializers.ModelSerializer):
class Meta:
model = models.Comment
fields = "__all__"
extra_kwargs = {
"content": {"error_messages": {"required": "评论内容不能为空"}},
"article": {"error_messages": {"required": "文章不能为空"}}
}
class SchoolSerializer(serializers.ModelSerializer):
class Meta:
model = models.School
fields = "__all__"
第一阶段:手垒代码
视图
from rest_framework.views import APIView
class SchoolView(APIView):
def get(self, request, *args, **kwargs):
query_set = models.School.objects.all()
ser_obj = app01_serializers.SchoolSerializer(query_set, many=True)
return Response(ser_obj.data)
class SchoolDetail(APIView):
def get(self, request, pk, *args, **kwargs):
obj = models.School.objects.filter(pk=pk).first()
ser_obj = app01_serializers.SchoolSerializer(obj)
return Response(ser_obj.data)
第二阶段:简单使用rest formwork自带混合类方法
视图
from rest_framework.generics import GenericAPIView # 公共类
from rest_framework.mixins import ListModelMixin, RetrieveModelMixin, CreateModelMixin # 混合类 必须与公共类搭配着使用
class SchoolView(GenericAPIView, mixins.ListModelMixin): # 查看学校列表
queryset = models.School.objects.all()
serializer_class = app01_serializers.SchoolSerializer
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)
class SchoolDetail(GenericAPIView, mixins.RetrieveModelMixin, mixins.CreateModelMixin): # 创建,与具体查出某一条记录
queryset = models.School.objects.all()
serializer_class = app01_serializers.SchoolSerializer
def get(self, request, pk, *args, **kwargs):
return self.retrieve(request, pk, *args, **kwargs)
def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)
路由
url(r'school/$', views.SchoolView.as_view()),
url(r'school/(?P<pk>\d+)/$', views.SchoolDetail.as_view()),
第三阶段:使用rest formwork自带的通用类方法
视图
from rest_framework.generics import GenericAPIView, ListCreateAPIView, RetrieveUpdateAPIVie
class SchoolView(ListCreateAPIView): # 创建
queryset = models.School.objects.all()
serializer_class = app01_serializers.SchoolSerializer
class SchoolDetail(RetrieveUpdateDestroyAPIView): # 更新
queryset = models.School.objects.all()
serializer_class = app01_serializers.SchoolSerializer
路由同上
第四阶段:使用rest formwork自带的封装所有放的类,代码量最少,功能最全
视图
from rest_framework.viewsets import ModelViewSet # 里面封装了操作的所有方法,增删改查查
class SchoolView(ModelViewSet):
queryset = models.School.objects.all()
serializer_class = app01_serializers.SchoolSerializer
路由
url(r'school/$', views.SchoolView.as_view(actions={
"get": "list",
"post": "create",
})),
url(r'school/(?P<pk>\d+)/$', views.SchoolView.as_view(actions={
'get': 'retrieve',
'put': 'update',
'patch': 'partial_update',
'delete': 'destroy'
})),
高级路由
视图
同上
路由
from rest_framework.routers import DefaultRouter
router = DefaultRouter()
router.register(r'school', views.SchoolView)
urlpatterns += router.urls
精华:流程图
来源:https://www.cnblogs.com/zpf666/p/10279293.html
标签:django,rest,framework,视图,路由
0
投稿
猜你喜欢
python 统计list中各个元素出现的次数的几种方法
2022-12-09 10:04:01
tensorflow实现softma识别MNIST
2021-02-17 22:32:56
python 判断自定义对象类型
2021-08-29 22:09:37
利用Python yagmail三行代码实现发送邮件
2021-10-16 08:39:04
Linux下安装mysql-5.6.4 的图文教程
2024-01-24 16:03:47
python进程间数据交互的几种实现方式
2022-11-05 09:32:12
python使用jieba实现中文分词去停用词方法示例
2021-02-04 11:27:17
关于数据库的alter table操作和索引概念
2009-12-15 21:57:00
mysql5.5与mysq 5.6中禁用innodb引擎的方法
2024-01-21 13:55:42
Codeigniter控制器controller继承问题实例分析
2023-11-23 04:15:31
SQL Server数据库分离和附加数据库的操作步骤
2024-01-27 19:59:23
整理Python最基本的操作字典的方法
2022-03-01 07:04:38
asp如何正确理解MIME类型?
2010-06-28 18:21:00
Python爬虫获取op.gg英雄联盟英雄对位胜率的源码
2024-01-02 13:03:52
如何创建SQL Server 2000故障转移群集
2024-01-23 17:00:23
python连接mysql并提交mysql事务示例
2024-01-15 04:43:37
python代码如何实现余弦相似性计算
2021-08-15 03:40:04
Python使用scipy保存图片的一些注意点
2023-12-01 06:06:58
javascript一些不错的函数脚本代码
2023-07-02 05:25:52
详解webpack require.ensure与require AMD的区别
2024-05-03 15:08:18