View of two base classes

View of two base classes

APIView

rest_framework.views.APIView

APIViewIs the base class for all views provided REST framework, Django inherited from the Viewparent class.

APIViewWith Viewthe exception that:

  • Passed to the view method is REST framework of Requestobject instead of Django HttpRequesetobjects;
  • The method may return REST framework view of Responsethe object, the view in response to the data set (the render) conform to the format required by the front end;
  • Any APIExceptionexceptions are captured, and information into appropriate response;
  • Performing dispatch () prior to distribution, will request authentication, authorization check, flow control.

Support defined attributes

  • authentication_classes list or Ganso, authentication type
  • permissoin_classes list or Ganso, permission checks class
  • throttle_classes lists or tuples, flow control class

In APIViewthe view definition is still a conventional method to achieve the class get (), post () method or other requests embodiment.

For example:

from rest_framework.views import APIView
from rest_framework.response import Response

# url(r'^books/$', views.BookListView.as_view()),
class BookListView(APIView):
    def get(self, request):
        books = BookInfo.objects.all()
        serializer = BookInfoSerializer(books, many=True)
        return Response(serializer.data)

GenericAPIView [General view class]

rest_framework.generics.GenericAPIView

Inherited from APIVIew, the main method of operation increases the serialization and database query role is to provide for the implementation of the following methods Mixin extension class support. Typically, when used, may be extended with one or more Mixin classes.

Properties and methods of use on a sequence provided

  • Attributes:

    • serializer_class specified serializer view used
  • method:

    • get_serializer_class(self)

      Occurs when a plurality of the view class calls the serializer, the condition can be determined by get_serializer_class process by returning a different sequence of class names can allow different views of a method of performing a sequence of objects.

      Back serializer class returned by default serializer_classcan be overridden, for example:

      def get_serializer_class(self):
          if self.request.user.is_staff:
              return FullAccountSerializer
          return BasicAccountSerializer
    • get_serializer(self, *args, **kwargs)

      Returns the serialized object, is mainly used to provide extended Mixin class to use, if we want to get serialized object in view, you can also call this method directly.

      Note that, when the method of providing a sequence of objects, attributes will be added to the context object serialization three data: request, format, view, these three data objects can be used to define the serializer.

      • request the current request object view
      • view class the current view object request
      • Returns the current request format desired data format

Properties and methods on database queries provided

  • Attributes:

    • queryset indicating that the data set used queries
  • method:

    • get_queryset(self)

      Return query set used by the view, is mainly used to provide extended Mixin class use, is the basis for access to data and list view details view, return to the default querysetattributes can be overridden, for example:

      def get_queryset(self):
          user = self.request.user
          return user.accounts.all()
    • get_object(self)

      Back Detail view data object model classes required, primarily to provide for the use of extension class Mixin.

      In an attempt can call this method to get the model class object details information.

      If the model class object details visit does not exist, it will return 404.

      This method checks whether the current object has permission to be accessed using check_object_permissions method APIView provided by default.

      For example:

      # url(r'^books/(?P<pk>\d+)/$', views.BookDetailView.as_view()),
      class BookDetailView(GenericAPIView):
          queryset = BookInfo.objects.all()
          serializer_class = BookInfoSerializer
      
          def get(self, request, pk):
              book = self.get_object() # get_object()方法根据pk参数查找queryset中的数据对象
              serializer = self.get_serializer(book)
              return Response(serializer.data)

Other properties that can be set

  • pagination_class specified class control tab
  • filter_backends specified backend control filter

Content from: https://www.cnblogs.com/xiaoyuanqujing

Guess you like

Origin www.cnblogs.com/SkyOceanchen/p/11922955.html