ViewSets and Routers
Creating a ModelViewSet
ModelViewSet combines the logic of listing, retrieving, creating, updating, and deleting objects for a model, using its queryset and serializer_class.
Example: Creating a ModelViewSet
ModelViewSet combines the logic of listing, retrieving, creating, updating, and deleting objects for a model, using its queryset and serializer_class.
from rest_framework import viewsets
from .models import Book
from .serializers import BookSerializer
class BookViewSet(viewsets.ModelViewSet):
queryset = Book.objects.all()
serializer_class = BookSerializer
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Registering with a Router
DefaultRouter inspects the ViewSet and generates all the standard URL patterns (list, detail, create, update, delete) automatically.
Example: Registering with a Router
DefaultRouter inspects the ViewSet and generates all the standard URL patterns (list, detail, create, update, delete) automatically.
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from .views import BookViewSet
router = DefaultRouter()
router.register('books', BookViewSet, basename='book')
urlpatterns = [
path('api/', include(router.urls)),
]
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
What URLs Get Generated
Registering BookViewSet under books produces a full set of RESTful endpoints without writing a single path() manually.
Example: What URLs Get Generated
Registering BookViewSet under books produces a full set of RESTful endpoints without writing a single path() manually.
GET /api/books/ -> list all books
POST /api/books/ -> create a book
GET /api/books/1/ -> retrieve book with id 1
PUT /api/books/1/ -> update book with id 1
DELETE /api/books/1/ -> delete book with id 1
⚠️ Run this command in your terminal.
- Writing separate URL patterns by hand for a ModelViewSet instead of registering it with a router, duplicating work the router already does.
- Confusing ViewSet with View — a ViewSet doesn't map directly to one URL, it maps to a set of related URLs generated by a router.
- Forgetting to include the router's generated urls (router.urls) in the project's URL configuration.
- ModelViewSet provides list, retrieve, create, update, and destroy actions from a single class.
- DefaultRouter automatically generates the URL patterns for a registered ViewSet, including a browsable API root.
- Registering a ViewSet with a router replaces writing each URL pattern by hand.
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: