Do not understand the mechanism of direct use to see the source code just fine, not hard, be able to able to understand
View family
1. View: The same method on the request to a view of a mapping class, in response to a request to complete (native Django)
from django.views import View
2. APIView(rest_framework)
from rest_framework.views import View
- Inheritance View, has all the features of View
- Rewrite as_views disabled csrf certification
- Rewrite dispatch: rendering abnormal response to a request to resolve the three major certification
- A bunch of multi-class attributes may be accomplished partial view class
3. GenricAPIView
from rest_framework.generics import GenericAPIView
- Inherits all the features APIView
- Method three: get_object () get_queryset () get_serializer ()
- Three properties: queryset serializer_class lookup_urk_kwarg
4. mixins package:
from rest_framework import mixins
from rest_framework.mixins import CreateModelMixin,UpdateModelMixin,RetrieveModelMixin,ListModelMixin,DestroyModelMixin
- 五大工具类 : RetrieveModelMixin , ListModelMixin, CreateModelMixin, UpdateModelMixin, DestroyModelMixin
- Six utility methods: retrieve, list, create, update, partial_update, destroy
5. generics follicles
from rest_framework import generics
- Mixins pile of tools in combination with the base class view GenericAPIView
6. ModelViewSet
View Set, inherits all the methods of all classes and mixins GenericAPIView
use:
#在view.py中 from rest_framework.viewsets import ModelViewSet class CarAPIView(ModelViewSet): queryset = models.car.objects.all() serializer_class = Serializers.CarSerializer #在urls.py中(这里的car只是用来做例子) urlpatterns = [ #以什么方式请求,就要在字典中,写上请求方式和请求的函数 url(r'^car/$',views.CarAPIView.as_view({'get':'list','post':'create'})), url(r'^car/(?P<pk>\d+)',views.CarAPIView.as_view({'get':'retrieve','put':'update','patch':'partial_update'})) ]