← Back to Chapters

API Views: APIView, GenericAPIView, ViewSet

? API Views: APIView, GenericAPIView, ViewSet

? Quick Overview

Django REST Framework provides multiple levels of abstraction for handling HTTP requests. APIView, GenericAPIView, and ViewSet help developers build APIs ranging from fully customized logic to fully automated CRUD endpoints.

? Key Concepts

  • APIView gives full control over HTTP methods
  • GenericAPIView simplifies CRUD with mixins
  • ViewSet groups related actions with routers

? APIView Class

APIView is the base DRF class where you manually define HTTP methods.

? View Code Example
# Simple APIView handling GET request
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status

class HelloWorld(APIView):
    def get(self, request):
        return Response({"message": "Hello, World!"}, status=status.HTTP_200_OK)

⚙️ GenericAPIView Class

GenericAPIView builds on APIView and works with mixins for common operations.

? View Code Example
# Generic views using built-in CRUD mixins
from rest_framework import generics
from .models import Book
from .serializers import BookSerializer

class BookListView(generics.ListCreateAPIView):
    queryset = Book.objects.all()
    serializer_class = BookSerializer

class BookDetailView(generics.RetrieveUpdateDestroyAPIView):
    queryset = Book.objects.all()
    serializer_class = BookSerializer

? ViewSet Class

ViewSet groups all CRUD operations into a single class and works with routers.

? View Code Example
# ModelViewSet handling full CRUD automatically
from rest_framework import viewsets
from .models import Book
from .serializers import BookSerializer

class BookViewSet(viewsets.ModelViewSet):
    queryset = Book.objects.all()
    serializer_class = BookSerializer

? Router Configuration

? View Code Example
# Registering ViewSet with DRF router
from rest_framework.routers import DefaultRouter
from .views import BookViewSet

router = DefaultRouter()
router.register(r'books', BookViewSet)

urlpatterns = router.urls

? Visual Flow

Client ➜ Router ➜ ViewSet ➜ Serializer ➜ Response

? Abstraction Simulator

Select a view type to see the code reduction vs. the JSON response.

Server Code (Python)

class BookList(APIView): def get(self, request): books = Book.objects.all() serializer = BookSerializer(books, many=True) return Response(serializer.data)

Simulated Response (JSON)

[ { "id": 1, "title": "Django for Beginners", "author": "Will Vincent" }, { "id": 2, "title": "Two Scoops of Django", "author": "Greenfeld & Roy" } ]

? Use Cases

  • APIView for custom logic APIs
  • GenericAPIView for standard CRUD
  • ViewSet for rapid API development

✅ Tips & Best Practices

  • Prefer ViewSets for clean and scalable APIs
  • Use APIView only when logic is unique
  • Combine permissions and serializers effectively

? Try It Yourself

  • Create a filter API using APIView
  • Build CRUD with GenericAPIView
  • Explore routers with multiple ViewSets