Django generic views

Django Generic Views

Django generic views are built of those base views and were developed as a shortcut for common usage patterns such as displaying the details of an object. They take certain common idioms and patterns found in view development and abstract them so that you can quickly write common views of data without having to repeat yourself. – Django documentation

Django’s generic views are a set of pre-built views provided by the Django framework to perform common tasks without the need for writing custom view functions from scratch. Generic views are designed to simplify the development process and reduce code duplication by providing generic implementations for common patterns.

One of the key benefits of class-based views is the way they allow you to compose bits of reusable behavior. REST framework takes advantage of this by providing a number of pre-built views that provide for commonly used patterns.

Concrete view classes

In Django REST the following classes are the concrete generic views. They are CreateAPIView, ListAPIView, RetriveAPIView, DestroyAPIView, UpdateAPIView, ListCreateAPIView, RetriveUpdateAPIView, RetriveDestroyAPIView, RetriveUpdateDestroyAPIView. The view classes can be imported from rest_framework.generics.

CreateAPIView

This is used to create only endpoints, which provide a POST method handler. It extends with GenericAPIView, CreateModelMixin.

class MyCustomMixin:
    queryset = Student.objects.all()
    serializer_class = StudentSerializer

class StudentCreate(MyCustomMixin, generics.CreateAPIView):
    "POST"

ListAPIView

Used for read-only endpoints to represent a collection of model instances, which provides a GET method handler. It extends with GenericAPIView, ListModelMixin.

class MyCustomMixin:
    queryset = Student.objects.all()
    serializer_class = StudentSerializer

class StudentList(MyCustomMixin, generics.ListAPIView):
    "GET"

RetrieveAPIView

Used for read-only endpoints to represent a single model instance, which provides a GET method handler. It extends with GenericAPIView, RetrieveModelMixin.

class MyCustomMixin:
    queryset = Student.objects.all()
    serializer_class = StudentSerializer

class StudentRetrieve(MyCustomMixin, generics.RetrieveAPIView):
    "GET"

DestroyAPIView

Used for delete-only endpoints to represent a single model instance, which provides a DELETE method handler. It extends with GenericAPIView, DestroyModelMixin.

class MyCustomMixin:
    queryset = Student.objects.all()
    serializer_class = StudentSerializer

class StudentDelete(MyCustomMixin, generics.DestroyAPIView):
    "DELETE"

UpdateAPIView

Used for update-only endpoints to represent a single model instance, which provides a PUT and PATCH method handler. It extends with GenericAPIView, UpdateModelMixin.

class MyCustomMixin:
    queryset = Student.objects.all()
    serializer_class = StudentSerializer

class StudentUpdate(MyCustomMixin, generics.UpdateAPIView):
    "UPDATE"

ListCreateAPIView

Used for read-write endpoints to represent a collection of model instances, which provides a GET and POST method handler. It extends with GenericAPIView, ListModelMixin, and CreateModelMixin.

class MyCustomMixin:
    queryset = Student.objects.all()
    serializer_class = StudentSerializer

class StudentListCreate(MyCustomMixin, generics.ListCreateAPIView):
    "LIST CREATE"

RetrieveUpdateAPIView

Used for read-update endpoints to represent a single model instance, which provides a GET, PUT, and PATCH method handler. It extends with GenericAPIView, RetrieveModelMixin, and UpdateModelMixin.

class MyCustomMixin:
    queryset = Student.objects.all()
    serializer_class = StudentSerializer

class StudentRetrieveUpdate(MyCustomMixin, generics.RetrieveUpdateAPIView):
    "RETRIEVE UPDATE"

RetrieveDestroyAPIView

Used for read-delete endpoints to represent a single model instance, which provides a GET, and DELETE method handler. It extends with GenericAPIView, RetrieveModelMixin, and DestroyModelMixin.

class MyCustomMixin:
    queryset = Student.objects.all()
    serializer_class = StudentSerializer

class StudentRetrieveDestroy(MyCustomMixin, generics.RetrieveDestroyAPIView):
    "RETRIEVE DELETE"

RetrieveUpdateDestroyAPIView

Used for read, write, and delete endpoints to represent a single model instance, which provides a GET, and, PUT, PATCH, and DELETE method handler. It extends with GenericAPIView, RetrieveModelMixin, UpdateModelMixin, and DestroyModelMixin.

class MyCustomMixin:
    queryset = Student.objects.all()
    serializer_class = StudentSerializer

class StudentRetrieveUpdateDestroy(MyCustomMixin, generics.RetrieveUpdateDestroyAPIView):
    "RETRIEVE UPDATE DESTROY"
Share this:

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top