python Django Rest_Framework框架 五个视图扩展类详解(图文并茂版)
Posted 温柔且上进c
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python Django Rest_Framework框架 五个视图扩展类详解(图文并茂版)相关的知识,希望对你有一定的参考价值。
五个视图扩展类详解
介绍
扩展类也叫混入类(Mixin)
作用:
-
提供了对数据资源进行增删改查的五种后端视图,当需要做出的操作数据此五种,可以通过继承相应的扩展类来复用代码,减少自己编写的代码量
-
这五个扩展类需要搭配GenericAPIView通用视图基类一起使用,因为五个扩展类的实现需要调用GenericAPIView提供的序列化器与数据库查询的方法
-
下述操作的数据模型均为前面博客中的同一模型,python Django Rest_Framework框架 APIView介绍与序列化器详解(图文并茂版)
ListModelMixin(列表视图扩展类)
-
列表视图扩展类,提供**list(request, *args, kwargs)方法快速实现列表视图,返回200状态码
-
该Mixin的list方法可以对数据进行过滤和分页
代码实现:
from rest_framework import status
from rest_framework.generics import GenericAPIView
from rest_framework.mixins import ListModelMixin
from rest_framework.response import Response
from rest_framework.views import APIView
from .models import Students
from .serilizers import StudentsSerializers, StudentModelSerializers
class StudentView(GenericAPIView, ListModelMixin):
"""ListModelMixin:展示全部数据"""
queryset = Students.objects.all()
serializer_class = StudentModelSerializers
def get(self, request):
return self.list(request)
路由:
from django.urls import path, re_path
from . import views
urlpatterns = [
# 基出序列化器
# 多数据操作
path('students/', views.StudentsApiview.as_view()),
# 单个数据进行操作
re_path('students/(?P<pk>\\d+)/', views.StudentDerailApiview.as_view()),
# 模型序列化器
path('students1/', views.Students1Apiview.as_view()),
re_path('students1/(?P<pk>\\d+)/', views.StudentDerail1Apiview.as_view()),
# GenericAPIView[通用视图类]与模型序列化器
path('students2/', views.StudentGenericAPIView.as_view()),
re_path('students2/(?P<pk>\\d+)/', views.StudentDetailGenericAPIView.as_view()),
# GenericAPIView[通用视图类]与扩展类
path('students3/', views.StudentView.as_view())
]
CreateModelMixin(创建视图扩展类)
-
创建视图扩展类,提供**create(request, *args, kwargs)方法快速实现创建资源的视图,成功返回201状态码
-
如果序列化器对前端发送的数据验证失败,返回400错误
代码实现:
from rest_framework import status
from rest_framework.generics import GenericAPIView
from rest_framework.mixins import ListModelMixin, CreateModelMixin
from rest_framework.response import Response
from rest_framework.views import APIView
from .models import Students
from .serilizers import StudentsSerializers, StudentModelSerializers
class StudentView(GenericAPIView, ListModelMixin, CreateModelMixin):
"""ListModelMixin:展示全部数据"""
queryset = Students.objects.all()
serializer_class = StudentModelSerializers
def get(self, request):
return self.list(request)
def post(self, request):
return self.create(request)
路由与上一个扩展类相同
RetrieveModelMixin(详情视图扩展类)
-
详情视图扩展类,提供**retrieve(request, *args, kwargs)方法,可以快速实现返回一个存在的模型数据对象
-
如果存在,返回200状态码, 否则返回404状态码
代码实现:
from rest_framework import status
from rest_framework.generics import GenericAPIView
from rest_framework.mixins import ListModelMixin, CreateModelMixin, RetrieveModelMixin, UpdateModelMixin
from rest_framework.response import Response
from rest_framework.views import APIView
from .models import Students
from .serilizers import StudentsSerializers, StudentModelSerializers
class StudentDetailView(GenericAPIView, RetrieveModelMixin):
"""RetrieveModelMixin:查看一个存在的数据对象
"""
queryset = Students.objects.all()
serializer_class = StudentModelSerializers
def get(self, request, pk):
return self.retrieve(request, pk)
路由文件:
# @Time: 2022/4/6-10:02
# @User: Ycx
from django.urls import path, re_path
from . import views
urlpatterns = [
# 基出序列化器
# 多数据操作
path('students/', views.StudentsApiview.as_view()),
# 单个数据进行操作
re_path('students/(?P<pk>\\d+)/', views.StudentDerailApiview.as_view()),
# 模型序列化器
path('students1/', views.Students1Apiview.as_view()),
re_path('students1/(?P<pk>\\d+)/', views.StudentDerail1Apiview.as_view()),
# GenericAPIView[通用视图类]与模型序列化器
path('students2/', views.StudentGenericAPIView.as_view()),
re_path('students2/(?P<pk>\\d+)/', views.StudentDetailGenericAPIView.as_view()),
# GenericAPIView[通用视图类]与扩展类
path('students3/', views.StudentView.as_view()),
re_path('students3/(?P<pk>\\d+)/', views.StudentDetailView.as_view()),
]
UpdateModelMixin(更新视图扩展类)
-
更新视图扩展类,提供**update(request, *args, kwargs)方法,可以快速实现更新一个存在的数据对象
-
同时也提供**partial_update(request, *args, kwargs)方法,可以实现局部更新
-
成功返回200状态码,序列化器校验数据失败时,返回400错误状态码
代码实现:
from rest_framework import status
from rest_framework.generics import GenericAPIView
from rest_framework.mixins import ListModelMixin, CreateModelMixin, RetrieveModelMixin, UpdateModelMixin
from rest_framework.response import Response
from rest_framework.views import APIView
from .models import Students
from .serilizers import StudentsSerializers, StudentModelSerializers
class StudentDetailView(GenericAPIView, RetrieveModelMixin, UpdateModelMixin):
"""RetrieveModelMixin:查看一个存在的数据对象
"""
queryset = Students.objects.all()
serializer_class = StudentModelSerializers
def get(self, request, pk):
return self.retrieve(request, pk)
def put(self, request, pk):
return self.update(request, pk)
- 查看是否修改成功
DestroyModelMixin(删除视图扩展类)
-
删除视图扩展类,提供**destroy(request, *args, kwargs)方法,可以快速实现删除一个存在的数据对象
-
成功返回204状态码,不存在返回404状态码
代码实现:
from rest_framework import status
from rest_framework.generics import GenericAPIView
from rest_framework.mixins import ListModelMixin, CreateModelMixin, RetrieveModelMixin, UpdateModelMixin,
DestroyModelMixin
from rest_framework.response import Response
from rest_framework.views import APIView
from .models import Students
from .serilizers import StudentsSerializers, StudentModelSerializers
class StudentDetailView(GenericAPIView, RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin):
"""RetrieveModelMixin:查看一个存在的数据对象
UpdateModelMixin:更新一个存在的数据对象
DestroyModelMixin:删除一个存在的数据对象
"""
queryset = Students.objects.all()
serializer_class = StudentModelSerializers
def get(self, request, pk):
return self.retrieve(request, pk)
def put(self, request, pk):
return self.update(request, pk)
def delete(self, request, pk):
return self.destroy(request, pk)
- 查看是否删除成功
以上是关于python Django Rest_Framework框架 五个视图扩展类详解(图文并茂版)的主要内容,如果未能解决你的问题,请参考以下文章
python 中 django 的问题-------- 请高人指点 尽量详细点哦 初学django