Day13-混入视图基类

1.GeneriAPIView

1.1 GenericAPIView是什么

继承自APIView,可以对视图和基类进行通用支持
GenericAPIView的导入

from rest_framework.generics import GenericAPIView

1.2 GenericAPIView内部属性

queryset = 列表所有查询的对象 指定结果集
serializers_class = 序列化器 指定序列化器
lookup_url_kwarg = ‘id’ 指定动态参数
lookup_field = ‘id’ 指定过滤的ORM的字段

2 .混入类与扩展类

2.1 混入类的功能划分

ListModelMixin
class ListView(ListModelMixin, GenericAPIView):
    queryset = People.objects.all()
    serializer_class = PeopleSerializers
    def get(self, request):
        return self.list(request)
CreateModelMixin
class CreateSer(serializers.PeoplelSerializer):
    class Meta:
        model = People
        fields = '__all__'
        
class CreateView(GenericAPIView, CreateModelMixin):
    serializer_class =PeopleSerializer
    def post(self, request):
        return self.create(request)
RetrieveModelMixin
class DetailView(GenericAPIView, RetrieveModelMixin):
    queryset = model.objects.all()
    serializer_class = PeopleSerializer
    lookup_field = 'id'
    lookup_url_kwarg = 'id'
    def get(self, request, id):
        return self.retrieve(request)
UpdateModelMixin
class UpdateView(GenericAPIView, UpdateModelMixin):
    queryset = model.objects.all()
    serializer_class = PeopleSerializer
    lookup_field = 'id'  # 被更新数据过滤字段
    lookup_url_kwarg = 'id'  # 被更新数据过滤条件参数值
    def put(self, request, id):
        return self.update(request)
DestroyModelMixin
class DeleteView(GenericAPIView, DestroyModelMixin):
    queryset = People.objects.all()
    def delete(self, request, id):
        return self.destroy(request)

3. 扩展类的应用

CreateAPIView
class PeopleCreateView(CreateAPIView):
    serializer_class = PeopleCreateSer
ListAPIView
class PeoplerListView(ListAPIView):
    queryset = People.objects.all()  # 你要序列化的哪些数据结果
    serializer_class = PeopleListSer  # 用什么序列化器
RetireveAPIView
class PeopleDetailView(RetrieveAPIView):
    lookup_field = 'id'  # 数据库里的字段
    lookup_url_kwarg = 'id'  # 过滤字段的条件,从路由传参数过来的
    queryset = People.objects.all()
    serializer_class = PeopleDetailSer
DestoryAPIView
class PeopleDestoryView(DestroyAPIView):
    lookup_field = 'pk'  # 数据库里的字段
    lookup_url_kwarg = 'id'  # 过滤字段的条件,从路由传参数过来的
    queryset = People.objects.all()
UpdateAPIView
  • 提供 put 和 patch 方法,可以更新或者局部更新某条数据

继承自:GenericAPIViewUpdateModelMixin

class PeopleUpdateView(UpdateAPIView):
    lookup_field = 'id'  # 数据库里的字段
    lookup_url_kwarg = 'id'  # 过滤字段的条件,从路由传参数过来的
    queryset = People.objects.all()
    serializer_class = PeopleUpdateSer
ListCreateAPIView
from rest_framework.generics import ListCreateAPIView,RetrieveUpdateDestroyAPIView
class PeopleView(ListCreateAPIView):
#   1.指定查询结果集
    queryset = People.objects.all()
#   2.指定序列化器
   serializer_class = PeopleSerializer2
RetrieveUpdateAPIView
class PeopleRetrieveUpdateView(RetrieveUpdateAPIView):
    lookup_field = 'id'  # 数据库里的字段
    lookup_url_kwarg = 'id'  # 过滤字段的条件,从路由传参数过来的
    queryset = People.objects.all()
    serializer_class = PeopleRetrieveUpdateSer
RetrieveDestroyAPIView
class PeopleRetrieveDestroyView(RetrieveDestroyAPIView):
    lookup_field = 'id'  # 数据库里的字段
    lookup_url_kwarg = 'id'  # 过滤字段的条件,从路由传参数过来的
    queryset = People.objects.all()
    serializer_class = PeopleDeatilSer 
RetrieveUpdateDestoryAPIView
 class PeopleView2(RetrieveUpdateDestroyAPIView):
     #1、指定查询结果集
     queryset = People.objects.all()
     # 2.指定序列化器
     serializer_class =  PeopleSerializer2
      # 3.指定动态参数
      lookup_url_kwarg = 'id'
      # 4.指定过滤的ORM的字段
      lookup_field = 'id'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值