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 方法,可以更新或者局部更新某条数据
继承自:GenericAPIView、UpdateModelMixin
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'