DRF ViewSets Tutorial: Router Actions and Per-Operation Policies
A ViewSet groups related resource actions rather than declaring each route's handler independently. DRF routers map supported actions to URL and method combinations. That convenience makes it important to inspect which operations are exposed and how querysets, serializers, and permissions vary when one class serves several actions.
Here's a popular DRF ViewSets video — watch it, then summarize it with one click. Or paste any other YouTube link below.
— or paste your own YouTube link —
Want to keep the key points from a YouTube video? Paste the link below to turn it into structured notes.
Why Use DRF ViewSets Tutorial: Router Actions and Per-Operation Policies
- Map list and detail actions explicitly
- Inspect router-generated resource paths
- Choose a restricted action set when appropriate
- Define custom actions with clear semantics
- Apply action-specific permissions deliberately
Frequently Asked Questions
Does every ViewSet expose all model CRUD operations?
The base class and included mixins determine available actions. Choose the intended combination rather than using a broad model viewset by default when the API should permit only reading or selected writes.
Why does a router need to identify a resource basename?
The router uses naming information when constructing route names, which matters for reversing and related API behavior. Supply the appropriate basename when it cannot infer one from the viewset's configuration.
How does an action differ from an HTTP method handler?
Actions describe resource operations such as list or retrieve, while routing maps them to method and path combinations. Understanding that mapping explains why several actions can be organized within one class.
Should a custom action bypass the authorized queryset?
It still needs the same record-scope and permission reasoning as ordinary actions. Reuse the appropriate selection mechanisms so a convenience method does not become an alternate unrestricted route to an object.
Can serializer choice differ between list and update actions?
A viewset can choose representations deliberately for different operations. Keep those contracts explicit and reflected in documentation so a compact list response is not mistaken for the complete accepted update payload.