Django-rest Framework(六)

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
  1. Inheritance View, has all the features of View
  2. Rewrite as_views disabled csrf certification
  3. Rewrite dispatch: rendering abnormal response to a request to resolve the three major certification
  4. A bunch of multi-class attributes may be accomplished partial view class

3. GenricAPIView

from rest_framework.generics import GenericAPIView
  1. Inherits all the features APIView
  2. Method three: get_object () get_queryset () get_serializer ()
  3. 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
  1. 五大工具类 : RetrieveModelMixin , ListModelMixin, CreateModelMixin, UpdateModelMixin, DestroyModelMixin
  2. Six utility methods: retrieve, list, create, update, partial_update, destroy

5. generics follicles

from rest_framework import generics
  1. Mixins pile of tools in combination with the base class view GenericAPIView

6. ModelViewSet

  1. View Set, inherits all the methods of all classes and mixins GenericAPIView

  2. 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'}))
    ]
    
    

Guess you like

Origin www.cnblogs.com/kuck/p/11921941.html