Click to jump to the REST-Framework column directory

GenericAPIView

GenericAPIView: GenericAPIView: GenericAPIView: GenericAPIView

#! /usr/bin/env python
# _*_ coding: UTF-8 _*_
from rest_framework.generics import GenericAPIView
Copy the code

In the source code, you can see that it is an extension of APIView. When you inherit this view class to write a custom view, you can obtain the following properties:

  • queryset: used to return query set objects from this view. This property must be set or overriddenget_queryset()Method, which must be called if you want to override view methodsget_queryset()Instead of accessing this property directly
  • serializer_class: Serialized class used to validate input and deserialize output, this property must be set or overriddenget_serializer_class()Methods.
  • lookup_field: Model field used to perform object lookups for individual model instances, default topk
  • lookup_url_kwarg: URL keyword argument for object lookup
#! /usr/bin/env python
# _*_ coding: UTF-8 _*_
from rest_framework import serializers
from rest_framework.generics import GenericAPIView
from rest_framework.permissions import IsAdminUser

from models import User


class MedusaSerializer(serializers.Serializer) :.class MedusaView(GenericAPIView) :
    queryset = User.objects.all()
    serializer_class = MedusaSerializer
    permission_classes = [IsAdminUser]
Copy the code

Of course, it may extend some of the methods on the APIView to make our API writing easier and more prescriptive, but its existence is more recommended to use with mixins,

  • perform_create(self, serializer)– CreateModelMixin Is called when a new object instance is saved
  • perform_update(self, serializer)– UpdateModelMixin is called when an existing object instance is saved
  • perform_destroy(self, instance)– DestroyModelMixin Called when an object instance is deleted

For example, if you need to do something else to save a new instance object, you can override it in perform_create(self, serializer) :

def perform_create(self, serializer) :
    instance = serializer.save()
    
    # a function or code block that you need to do any other processing.Copy the code

You can also revalidate the updated data by raising ValidationError() :

def perform_update(self, serializer) :
    if 1= =1:
        raise ValidationError('I don't care, I just won't let you update. ')
    serializer.save(user=self.request.user)
Copy the code

Of course, there’s a ListModelMixin to get a data set, a RetrieveModelMixin to get a data detail, and most of the requests and the requests are outlined.

ListModelMixin

List (request, *args, **kwargs) is provided to query a collection of objects. You can also override its list method to implement your own return body. You can also pagination it (pagination will be covered later). Will return 200 OK status code to you:

#! /usr/bin/env python
# _*_ coding: UTF-8 _*_
from rest_framework.generics import GenericAPIView
from rest_framework.mixins import ListModelMixin
from rest_framework.permissions import IsAdminUser


class MedusaView(GenericAPIView, ListModelMixin) :
    queryset = User.objects.all()
    serializer_class = MedusaSerializer
    permission_classes = [IsAdminUser]
Copy the code

CreateModelMixin

Provides a.create(request, *args, **kwargs) method that creates and stores a new instance object of the model. If the object is successfully Created, a 201 Created response is returned. If the request data provided to create the object is invalid, A 400 Bad Request response is returned with the error details as the body of the response.

#! /usr/bin/env python
# _*_ coding: UTF-8 _*_
from rest_framework.generics import GenericAPIView
from rest_framework.mixins import CreateModelMixin
from rest_framework.permissions import IsAdminUser


class MedusaView(GenericAPIView, CreateModelMixin) :
    queryset = User.objects.all()
    serializer_class = MedusaSerializer
    permission_classes = [IsAdminUser]
Copy the code

RetrieveModelMixin

Provides the.Retrieve (Request, *args, **kwargs) method, which implements a response that returns details of the existing model instance, if the object can be retrieved, a 200 OK response, otherwise 404 Not Found.

#! /usr/bin/env python
# _*_ coding: UTF-8 _*_
from rest_framework.generics import GenericAPIView
from rest_framework.mixins import RetrieveModelMixin
from rest_framework.permissions import IsAdminUser


class MedusaView(GenericAPIView, RetrieveModelMixin) :
    queryset = User.objects.all()
    serializer_class = MedusaSerializer
    permission_classes = [IsAdminUser]
Copy the code

UpdateModelMixin

Provides.update(request, *args, **kwargs) methods that update and save instances of existing models, A.partial_update(request, *args, **kwargs) method is also provided, which is similar to the update method, but all fields used for the update are optional, This will support HTTP PATCH requests. If the object is updated, it will return a 200 OK response. If the Request data provided to update the object is invalid, a 400 Bad Request response is returned with the error details as the body of the response.

#! /usr/bin/env python
# _*_ coding: UTF-8 _*_
from rest_framework.generics import GenericAPIView
from rest_framework.mixins import UpdateModelMixin
from rest_framework.permissions import IsAdminUser


class MedusaView(GenericAPIView, UpdateModelMixin) :
    queryset = User.objects.all()
    serializer_class = MedusaSerializer
    permission_classes = [IsAdminUser]
Copy the code

DestroyModelMixin

Provide.destroy(Request, *args, **kwargs) methods that delete existing model instances and return 204 No Content if the object is removed successfully or 404 Not Found otherwise.

#! /usr/bin/env python
# _*_ coding: UTF-8 _*_
from rest_framework.generics import GenericAPIView
from rest_framework.mixins import DestroyModelMixin
from rest_framework.permissions import IsAdminUser


class MedusaView(GenericAPIView, DestroyModelMixin) :
    queryset = User.objects.all()
    serializer_class = MedusaSerializer
    permission_classes = [IsAdminUser]
Copy the code

Composite view classes

  • CreateAPIView

    View for creating only new model instance objects

    providePOSTRequest way

    Rely on:GenericAPIView.CreateModelMixin
  • ListAPIView

    A view for read-only model instance object collection data

    provideGETRequest way

    Rely on:GenericAPIView.ListModelMixin
  • RetrieveAPIView

    Views that read only a single model instance object

    provideGETRequest way

    Rely on:GenericAPIView.RetrieveModelMixin
  • DestroyAPIView

    A view used to delete only a single model instance object

    provideDELETERequest way

    Rely on:GenericAPIView.DestroyModelMixin
  • UpdateAPIView

    Views used to modify only existing model instances

    providePUTPATCHRequest way

    Rely on:GenericAPIView.UpdateModelMixin
  • ListCreateAPIView

    A view for reading and writing to a set of model instance objects

    provideGETPOSTRequest way

    Rely on:GenericAPIView.ListModelMixin.CreateModelMixin
  • RetrieveUpdateAPIView

    Views for reading and updating individual model instances

    provideGET,PUTPATCHRequest way

    Rely on:GenericAPIView.RetrieveModelMixin.UpdateModelMixin
  • RetrieveDestroyAPIView

    Views for reading and deleting individual model instances

    provideGETDELETERequest way

    Rely on:GenericAPIView.RetrieveModelMixin.DestroyModelMixin
  • RetrieveUpdateDestroyAPIView

    Views for reading, updating, and deleting individual model instances

    provideGET,PUT,PATCHDELETERequest way

    Rely on:GenericAPIView.RetrieveModelMixin.UpdateModelMixin.DestroyModelMixin