视图家族
目录
视图家族
**1.View: **
- 将请求方式与视图类的同名方法建立映射,完成请求响应
2.APIView
- 拥有View的所有功能
- 重写as_view禁用csrf认证
- 重写dispatch: 请求、响应、渲染、异常、解析、三个认证
- 多了一堆类属性,可以完成试图类地局部配置
eg:
class StudentAPIView(APIView):
def get(self, request, *args, **kwargs):
stu_query = models.Student.objects.all()
stu_ser = serializers.StudentModelSerializer(stu_query, many=True)
return Response(stu_ser.data)
3.GenericAPIView:
- 拥有APIView的所有功能
- 三个方法: get_object()、get_queryset()、get_serializer()
- 三个属性: queryset、serializer_class、lookup_url_kwargs
eg:
class StudentGenericAPIView(GenericAPIView):
queryset = models.Student.objects.all()
serializer_class = serializers.StudentModelSerializer
def get(self, request, *args, **kwargs):
# stu_query = models.Student.objects.all()
stu_query = self.get_queryset()
# stu_ser = serializers.StudentModelSerializer(stu_query, many=True)
stu_ser = self.get_serializer(stu_query, many=True)
return Response(stu_ser.data)
4.mixins包:
- 五大工具类: RetrieveModelMinxin , ListModelMixin , CreateModelMixin , UpdateModelMixin,
- 六大工具方法:retrieve , list , create, update , partial_update , destroy
from rest_framework import mixins
class StudentMixinGenericAPIView(mixins.ListModelMixin, mixins.CreateModelMixin, GenericAPIView):
queryset = models.Student.objects.all()
serializer_class = serializers.StudentModelSerializer
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)
5.generics包:
- 一堆mixins工具类与GenericAPIView视图基类组合
from rest_framework.generics import CreateAPIView, RetrieveAPIView, ListAPIView, UpdateAPIView, DestroyAPIView
class StudentMixinAPIView(CreateAPIView, RetrieveAPIView, ListAPIView, UpdateAPIView, DestroyAPIView):
queryset = models.Student.objects.all()
serializer_class = serializers.StudentModelSerializer
# url中单查,不一定必须提供主键,提供一切唯一键的字段名均可
lookup_url_kwarg = 'id'
# 有删除需求的接口继承DestroyAPIView,重写destroy完成字段删除
def destroy(self, request, *args, **kwargs):
pass
6.ViewSetMixin
- 重写as_view(), 完成请求方式与视图方法的自定义映射
- 例子: as_view({'get': 'my_get'})
# 视图集
from rest_framework.viewsets import ModelViewSet
class StudentModelViewSet(ModelViewSet):
queryset = models.Student.objects.all()
serializer_class = serializers.StudentModelSerializer
def my_post(self, request, *args, **kwargs):
return Response('my post ok') # 需要在url中做映射
7.视图集基类:
- ViewSet(ViewSetMixin, APIView): 可以自定义映射关系的APIView
- GeneriViewSet(ViewSetMixin, GenericAPIView): 可以自定义映射关系的GenericAPIView
8.常用Model视图集
-
ModelViewSet(mixins.CreateModelMixin,
mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin,
mixins.ListModelMixin,
GenericViewSet)
(某一资源的六大操作视图集) -
ReadOnlyModelViewSet(mixins.RetrieveModelMixin,
mixins.ListModelMixin,
GenericViewSet)
(某一资源的只读操作视图集)
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(三):用.NET IoT库
· 【非技术】说说2024年我都干了些啥