Introduction to the
APIView and Viewset are View classes introduced in DRF. The Viewset encapsulates the DRF’s own APIView, which encapsulates a native Django View. Flexible use of APIView and Viewset can greatly improve business development efficiency and code maintainability.
The three difference
Inheritance relationships
ViewSet(GenericViewSet) -drf
APIView(GenericAPIView) -drf
View -django
Copy the code
“The View”
Derived from Django native, it is the parent of all class-based views, and is responsible for connecting views to urls, HTTP method scheduling (GET,POST, etc.), and other simple functions.
【 APIView 】
The APIView is the parent of all DRF views and itself inherits from Djangos views with simple scheduling methods and checks.
“ViewSet”
ViewSet inherits APIView, adds.as_view() and.initialize_request(), and can map routes with the router
The difference between the three
The most important difference between the three is the mixin
APIView and View
Get (), POST (), or other request-style methods are still implemented in the APIView using the normal class-view definition methods
Added custom attributes
authentication_classes
List or meta-ancestor, authentication classpermissoin_classes
List or meta-ancestor, permission checking classthrottle_classes
List or meta-ancestor, flow control class
Requests and returns using DRF's Request Response instead of Django's HttpResponse. HttpResponse authenticates requests as they come in. Any APIException will be caught and placed in the appropriate desired. The content returned in response must be serialized JSONCopy the code
GenericAPIView and APIView
GenericAPIView inherits from APIView
GenericAPIView New configuration item
-
The list view is used with the details view
- Queryset specifies the range of model data to be used.
- Serializer_class Sets the serializer used by the view
-
[List view] Use
- Pagination_class sets paging
- Filter_backends Sets filtering
-
[Detail page view] Use
- Lookup_field Conditional field to use when querying a single database object, default to ‘PK’
- Lookup_url_kwarg Parameter keyword name in THE URL when querying single data. The default value is the same as look_field
GenericAPIView new method
- Get_queryset () returns the queryset used by the list view, which is the basis for the list view and the detail view to obtain data. This returns the queryset property by default, and can be overridden, for example:
def get_queryset(self):
user = self.request.user
return user.accounts.all()
Copy the code
- Get_serializer_class () returns the serializer class. Serializer_class is returned by default, and can be overridden, for example:
def get_serializer_class(self):
if self.request.user.is_staff:
return FullAccountSerializer
return BasicAccountSerializer
Copy the code
-
Get_serializer (self, args, *kwargs) get_serializer(self, args, *kwargs) returns the serializer object. It is used by other views or extension classes. Note that when providing the serializer object, the REST Framework adds three data objects to the context property of the object: Request, Format, and View, which can be used when defining the serializer.
-
Get_object () returns the model-class data object required by the detail view. By default, lookup_field is used to filter querySets. Model class objects that can be called in an attempt to get detailed information. If the model class object accessed by the detail does not exist, 404 is returned. This method defaults to using the check_object_permissions method provided by the APIView to check whether the current object has permissions to be accessed.
# url(r'^books/(? P
\d+)/$', views.BookDetailView.as_view()),
class BookDetailView(GenericAPIView):
queryset = BookInfo.objects.all()
serializer_class = BookInfoSerializer
def get(self, request, pk):
book = self.get_object()
serializer = self.get_serializer(book)
return Response(serializer.data)
Copy the code
- get_serializer_context()
- filter_queryset()
GenericAPIView can be a combination of a mixin mixins. CreateModelMixin mixins. ListModelMixin mixins. RetrieveModelMixin Mixins. UpdateModelMixin Mixins. DestroyModelMixin GenericAPIView Mixin that has been combined (in the same directory as GenericAPIView) CreateAPIView = GenericAPIView + mixins.CreateModelMixin (POST has create method) ListAPIView = GenericAPIView + mixins.ListModelMixin (GET has The list method) RetrieveAPIView = GenericAPIView + mixins. RetrieveModelMixin DestroyAPIView = (GET retrieve method) GenericAPIView + mixins.DestroyModelMixin UpdateAPIView = GenericAPIView + mixins.UpdateModelMixin ListCreateAPIView = GenericAPIView + mixins.ListModelMixin + mixins.CreateModelMixin RetrieveUpdateAPIView = GenericAPIView + mixins.RetrieveModelMixin + mixins.UpdateModelMixin RetrieveDestroyAPIView = GenericAPIView + mixins.RetrieveModelMixin + mixins.DestroyModelMixin RetrieveUpdateDestroyAPIView = GenericAPIView + mixins.RetrieveModelMixin + mixins.DestroyModelMixin + mixins.DestroyModelMixinCopy the code
ViewSet and APIView
The difference between ViewSet and APIView
ViewSet inherits from APIView with a ViewSetMixin and a.as_view() method, Initialize_request () initialize_Request () initialize_request() initialize_request() to request() Binding lots of actions, mainly used in dynamic serializers
GenericViewSet and ViewSet
GenericViewSet inherits from GenericAPIView
GenericViewSet Mixins that have been composed (in the same directory as GenericViewSet)
ReadOnlyModelViewSet = GenericViewSet + mixins.RetrieveModelMixin + mixins.ListModelMixin
ModelViewSet = GenericViewSet + mixins.CreateModelMixin + mixins.RetrieveModelMixin + UpdateModelMixin + mixins.DestroyModelMixin + mixins.ListModelMixin
Copy the code
The introduction of Viewset
from rest_framework import viewsets
Copy the code
Viewsets contain classes (common)
0.ViewSetMixin
Brief introduction: The base class for Viewset, which overrides the.as_view() method in Django Views to make registering urls easier. A native Django View achieves its implementation logic by overriding the concrete views of the GET and POST methods.
view = MyViewSet.as_view({'get': 'list'.'post': 'create'})
Copy the code
Specifies the function to call for the request