Generic views: ListCreateAPIView and RetrieveUpdateDestroyAPIView
TaskListView and TaskDetailView from part 7 both follow a pattern DRF has already automated: query a model, serialize it, handle the standard CRUD operations. Generic views are that automation — the same relationship ListView/DetailView have to a plain Django view, applied to an API.
ListCreateAPIView
# tasks/views.py
from rest_framework.generics import ListCreateAPIView
from .models import Task
from .serializers import TaskSerializer
class TaskListView(ListCreateAPIView):
queryset = Task.objects.all()
serializer_class = TaskSerializer
def perform_create(self, serializer):
serializer.save(owner=self.request.user)ListCreateAPIView handles GET (list) and POST (create) with queryset and serializer_class as the only two things to declare — the exact get()/post() methods written by hand in part 7 are gone. perform_create() is the hook for anything that needs to happen alongside saving a valid object — here, setting owner, the same job form_valid() did for CreateView in the Django from scratch series.
RetrieveUpdateDestroyAPIView
from rest_framework.generics import RetrieveUpdateDestroyAPIView
class TaskDetailView(RetrieveUpdateDestroyAPIView):
queryset = Task.objects.all()
serializer_class = TaskSerializerThis one class replaces every method TaskDetailView had in part 7 — GET (retrieve), PUT/PATCH (update), and DELETE (destroy) — with zero method bodies of your own. The URLconf from part 7 doesn't need to change at all; both classes still get wired up with .as_view() exactly the same way.
The generic view family
ListAPIView— read-only list, no createRetrieveAPIView— read-only single object, no update or deleteCreateAPIView— create onlyListCreateAPIView— list + create (used above)RetrieveUpdateAPIView— read + update, no deleteRetrieveDestroyAPIView— read + delete, no updateRetrieveUpdateDestroyAPIView— the full set (used above)
Each name directly describes which operations it supports — picking the narrowest one that fits a given endpoint is itself a form of access control: a ListAPIView for a read-only public endpoint makes an accidental POST handler structurally impossible, not just unwritten.
Reaching for the full RetrieveUpdateDestroyAPIView out of habit on an endpoint that should only ever be read from. Using the narrower RetrieveAPIView instead isn't just cleaner — it means there's no update/destroy method sitting there to accidentally get exposed later by a permissions misconfiguration (part 16).
Generic views remove boilerplate for the common case — but "list every task" and "list one user's tasks" are still two different classes with copy-pasted queryset/serializer_class. Next: ViewSet, which collapses an entire resource's list-detail-create-update-delete into one class.