0909 self-summary
drf framework in all views and usage
A frame all .drf view class
from django.views import View
from rest_framework import views, generics, mixins, viewsets
最基础
: Django comes with views likedrf框架中最基础的视图类
:views.APIViewdrf框架中工具视图
:generics- GenericAPIView: base view class of the class
- mixins view toolset:
- RetrieveModelMixin:retrieve 单取
- ListModelMixin:list 群取
- CreateModelMixin: create monocytogenes
- UpdateModelMixin: update a single overall change
- UpdateModelMixin: partial_update locally modified single
- DestroyModelMixin:destroy 单删
viewsets set of views
上述视图都是基于django自带的views进行相关方法的分装
Two .viewsets set of views
ViewSetMixin:视图集工具 - 重写as_view - 将 请求方式 映射到视图类中的 指定方法
我们在路由中 类名.as_view({'get': 'retrieve', 'delete': 'remove_obj'})
GenericViewSet:与模型类有关的接口视图集 - 可以从mixins那继承功能,也可以自定义功能
ViewSet:与模型类无关或不是标准模型类接口 - 一般都是自定义功能
Three .generics use in GenericAPIView
The serializer_class queryset and packaged as such property, there is provided a method of three
self.get_queryset (): Gets all
self.get_object (): Get Get The single pk
self.get_serializer (* args, ** kwargs): Gets the specified
Commonly used several parameters
- data: the sequence of parameter passing for returning
- many: acquiring a plurality of sequences of objects
- instance: If there are save to perform the update method behind, create a method not implemented
- partial: True to local field can modify the default is
False
changed to all fields
An important prerequisite for the use of these methods must be
queryset
serializer_class
- If we want to use get_object () must send us a front-end
pk
Four .mixins use
mixins, then in a method of dispensing into five generics method
- RetrieveModelMixin:retrieve 单取
- ListModelMixin:list 群取
- CreateModelMixin: create monocytogenes
- UpdateModelMixin: update a single overall change
- UpdateModelMixin: partial_update locally modified single
- DestroyModelMixin:destroy 单删
Use allself.方法名(request,*args,**kwargs)
The monosubstituted
First, we want to inherit class class class name (mixins.RetrieveModelMixin)
self.retrieve(request,*args,**kwargs)
But time and then perform these operations still give the same view with generics two parameters, and create a view class must inherit its parent class method of the class
Five .generics subclass inherits GenericAPIView use
class BookRetrieveUpdateAPIView(generics.RetrieveUpdateAPIView):
queryset = models.Book.objects.filter(is_delete=False).order_by('-id')
serializer_class = serializers.BookModelSerializer
In fact, the foundation again before another method of packaging
There are as many ways of looking at what is left to see the source code to source code ctrl +
Six custom view
本来get他就只会找类中get方法我们可以对其设置让他找我们对于的名称
路由层
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^v4/books/(?P<pk>.*)/$', views.BookGenericViewSet.as_view({
'delete': 'remove_book'
})),
]
视图层
class BookGenericViewSet(mixins.RetrieveModelMixin, mixins.ListModelMixin, mixins.CreateModelMixin, mixins.UpdateModelMixin, viewsets.GenericViewSet):
queryset = models.Book.objects.filter(is_delete=False).order_by('-id')
serializer_class = serializers.BookModelSerializer
def remove_book(self, request, *args, **kwargs):
pk = kwargs.get('pk')
try:
book_obj = models.Book.objects.get(is_delete=False, pk=pk)
book_obj.is_delete = True
book_obj.save()
return APIResponse(0, '删除成功')
except:
return APIResponse(1, '删除失败')