django---mixins模块及其GenericAPIView类源码分析
Posted 易辰_
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了django---mixins模块及其GenericAPIView类源码分析相关的知识,希望对你有一定的参考价值。
在简单分析mixins之前,我们先来简单看下如何使用?
简单使用
url配置如下:
url(r"books/$",views.BookView.as_view()),
url(r"bookdetail/(?P<pk>\\d+)/$",views.BookViewDetail.as_view()),
model类解析,用来校验请求参数的
class BookSerializers(serializers.ModelSerializer):
class Meta:
model=Book
fields="__all__"
视图类
BookView是对book表的所有记录查询、增加某些数据记录
BookViewDetail是对book表单条数据的查询、修改、删除
from rest_framework import mixins
from rest_framework import generics
class BookView(mixins.ListModelMixin,
mixins.CreateModelMixin,
generics.GenericAPIView):
queryset = Book.objects.all()
serializer_class = BookSerializers
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)
class BookViewDetail(
mixins.RetrieveModelMixin,
mixins.DestroyModelMixin,
mixins.UpdateModelMixin,
generics.GenericAPIView
):
queryset = Book.objects.all()
serializer_class = BookSerializers
def get(self,request, *args, **kwargs):
return self.retrieve(request, *args, **kwargs)
def delete(self,request, *args, **kwargs):
return self.destroy(request, *args, **kwargs)
def put(self,request, *args, **kwargs):
return self.update(request, *args, **kwargs)
我这里就不去测试了,直接简单分析源码,如果看测试结果可以去APIView、mixins、generics、HyperlinkedIdentityField了解
BookView
类多重继承了mixins.ListModelMixin、 mixins.CreateModelMixin、generics.GenericAPIView
,用来进行验证request请求,从而对book
表的数据集合的查看、添加数据操作
BookViewDetail类,多重继承了mixins.RetrieveModelMixin、mixins.DestroyModelMixin、mixins.UpdateModelMixin,、 generics.GenericAPIView
用来进行验证request请求,从而对book
表进行单条数据查看、修改、删除操作
我们先看mixins
模块,该模块有如下几个类
class CreateModelMixin(object):增加
class ListModelMixin(object):展示数据列表
class RetrieveModelMixin(object):展示单条数据
class UpdateModelMixin(object):更新单条数据
class DestroyModelMixin(object):删除单条数据
在分析源码前,我们先来了解多重继承的一个知识点
class A:
def a(self):
print("A")
self.b()
class B:
def b(self):
print("B")
class D(A,B):
pass
d = D()
d.a()
会输出AB
类D多重继承AB,类D实例对象d.a()时候,如果在类A中找不到b()方法,它就会在多重继承的AB类中,横向寻找b方法,结果在类B中找到,然后输出
GenericAPIView类
我在APIView源码分析,分析了APIView的大概执行流程,我们先看下多重继承类中的generics.GenericAPIView
,看看它跟APIView
有何关联?点开源码,原来GenericAPIView
类是APIView
的子类,继而扩充了一些其他的方法,我举出如下几个方法
get_queryset:提供方法,用来获取request请求封装完毕的结果集
get_object:获取单条数据
get_serializer:获取序列化后的数据
get_serializer_class:获取需要序列化的model类
get_serializer_context:获取序列化的数据,定义了某种格式的字典
paginator:分页器
在BookView
中依然按着APIView
的执行流程,通过请求方式,诸如get\\post等,当request
发起请求时,调用dispatch
然后执行某个get\\post
,我们自己定义好的入口类方法,
ListModelMixin分析
我们就以http://127.0.0.1:8000/books/(get方法)来举例,会执行如下代码:
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)
该方法直接返回了self.list(request, *args, **kwargs)
,
它会理所应当的去多重继承的父类中,去找寻方法,然后执行,跟踪代码会进入到ListModelMixin
类
class ListModelMixin(object):
"""
List a queryset.
"""
def list(self, request, *args, **kwargs):
queryset = self.filter_queryset(self.get_queryset())
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)
serializer = self.get_serializer(queryset, many=True)
return Response(serializer.data)
ListModelMixin
类list
方法是获取了queryset = self.filter_queryset(self.get_queryset())
,这个queryset
是在GenericAPIView
类中,我前面列举了一些该类的方法,
然后在判断是否分页,最后获取self.get_serializer(queryset, many=True)
序列化类,最后将序列化后的结果serializer.data
作为返回return Response(serializer.data)
返回即可
以上就是http://127.0.0.1:8000/books/(get方法)查询所有的记录
CreateModelMixin分析
对应BookView里面的post方法代码如下,流程大同小异
http://127.0.0.1:8000/books/(post方法)添加记录
def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)
跟进如下代码:
class CreateModelMixin(object):
"""
Create a model instance.
"""
def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
def perform_create(self, serializer):
serializer.save()
def get_success_headers(self, data):
try:
return 'Location': str(data[api_settings.URL_FIELD_NAME])
except (TypeError, KeyError):
return
依然是从GenericAPIView
多重继承类中获取序列化器,然后进行校验request
请求的json数据或者form数据,然后进行serializer.save()
用户Token
验证,并且通过return 'Location': str(data[api_settings.URL_FIELD_NAME])
保存一个临时头部数据,最后
将序列化的数据,响应状态码(201 Created:请求已经被实现)、头部一并返回即可
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
以上就是http://127.0.0.1:8000/books/(post方法)添加操作
RetrieveModelMixin分析
接下来简单看下单条数据的获取、更新、删除,先把视图代码贴出来
class BookViewDetail(
mixins.RetrieveModelMixin,
mixins.DestroyModelMixin,
mixins.UpdateModelMixin,
generics.GenericAPIView
):
queryset = Book.objects.all()
serializer_class = BookSerializers
def get(self,request, *args, **kwargs):
ret = self.get_object()
print("ret~~~~~~~~~", ret)
return self.retrieve(request, *args, **kwargs)
def delete(self,request, *args, **kwargs):
return self.destroy(request, *args, **kwargs)
def put(self,request, *args, **kwargs):
return self.update(request, *args, **kwargs)
执行流程也是大同小异
获取单条数据时候,http://127.0.0.1:8000/bookdetail/7(get请求)
执行get
方法也是从GenericAPIView
多重继承类中获取get_object
,然后进行序列化,最后 return Response(serializer.data)
返回即可
class RetrieveModelMixin(object):
"""
Retrieve a model instance.
"""
def retrieve(self, request, *args, **kwargs):
instance = self.get_object()
serializer = self.get_serializer(instance)
return Response(serializer.data)
UpdateModelMixin分析
接下来看下更新操作http://127.0.0.1:8000/bookdetail/7(put请求)
def put(self,request, *args, **kwargs):
return self.update(request, *args, **kwargs)
class UpdateModelMixin(object):
"""
Update a model instance.
"""
def update(self, request, *args, **kwargs):
partial = kwargs.pop('partial', False)
instance = self.get_object()
serializer = self.get_serializer(instance, data=request.data, partial=partial)
serializer.is_valid(raise_exception=True)
self.perform_update(serializer)
if getattr(instance, '_prefetched_objects_cache', None):
# If 'prefetch_related' has been applied to a queryset, we need to
# forcibly invalidate the prefetch cache on the instance.
instance._prefetched_objects_cache =
return Response(serializer.data)
def perform_update(self, serializer):
serializer.save()
def partial_update(self, request, *args, **kwargs):
kwargs['partial'] = True
return self.update(request, *args, **kwargs)
意思通过instance = self.get_object()
获取要修改的数据,然后get_serializer
拿到序列化器、进行请求数据合法校验、然后通过Token
进行用户验证、然后将更新好的数据返回return Response(serializer.data)
DestroyModelMixin分析
接下来看下删除操作http://127.0.0.1:8000/bookdetail/7(delete删除请求)
def delete(self,request, *args, **kwargs):
return self.destroy(request, *args, **kwargs)
class DestroyModelMixin(object):
"""
Destroy a model instance.
"""
def destroy(self, request, *args, **kwargs):
instance = self.get_object()
self.perform_destroy(instance)
return Response(status=status.HTTP_204_NO_CONTENT)
def perform_destroy(self, instance):
instance.delete()
通过pk(或者其他标识)获取要删除的数据,然后instance.delete()
删除,最后返回return Response(status=status.HTTP_204_NO_CONTENT)
HTTP 204(no content)表示响应执行成功,但没有数据返回,浏览器不用刷新
以上就是mixins模块源码分析
利用generics
我们还可以利用如下更简单的方式
from rest_framework import generics
class BookView(generics.ListCreateAPIView):
queryset = Book.objects.all()
serializer_class = BookSerializers
class BookViewDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Book.objects.all()
serializer_class = BookSerializers
我们就看BookView
即可原来继承如下的类,进行了再次的封装,代码更精简
class ListCreateAPIView(mixins.ListModelMixin,
mixins.CreateModelMixin,
GenericAPIView):
"""
Concrete view for listing a queryset or creating a model instance.
"""
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)
以上是关于django---mixins模块及其GenericAPIView类源码分析的主要内容,如果未能解决你的问题,请参考以下文章