0Pricing
Django Academy · Lesson

ModelViewSet and Routers

Generate CRUD routes automatically.

ModelViewSet and Routers is a free Django Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Django Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The CRUD Repetition Problem

Writing list, create, retrieve, update, and delete by hand means a lot of near-identical code. A ViewSet bundles all of it into one tidy class.

Meet ModelViewSet

The ModelViewSet gives you full CRUD for a model with almost no code. Point it at a queryset and a serializer, and it handles the rest.

from rest_framework import viewsets

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

Two Attributes Do the Work

Those two lines carry the whole class. queryset says which rows are in play, and serializer_class says how to turn them into JSON. 📦

The Actions You Get Free

A ModelViewSet provides five actions: list, create, retrieve, update, partial_update, and destroy. You wrote none of them by hand.

Why Routers Exist

A ViewSet groups actions, but URLs still need wiring. A router reads your ViewSet and generates every URL pattern automatically.

Registering with DefaultRouter

Create a DefaultRouter, register your ViewSet under a prefix, and it builds the full URL set for you.

from rest_framework.routers import DefaultRouter

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

Plugging Router URLs In

The router exposes router.urls, a ready list of patterns. Add it to your urlpatterns and the endpoints go live instantly.

urlpatterns = [
    path('api/', include(router.urls)),
]

The URLs You Just Created

Registering one ViewSet gives you /books/ for the list and create, plus /books/{id}/ for retrieve, update, and delete. 🚀

HTTP Verbs Map to Actions

The router maps verbs to actions: GET lists, POST creates, GET on an id retrieves, PUT or PATCH updates, and DELETE destroys. One ViewSet, six behaviors.

DefaultRouter vs SimpleRouter

DefaultRouter adds a handy API root view that lists your endpoints. SimpleRouter skips it when you want a leaner URL set.

Adding a Custom Action

Need an extra endpoint? The @action decorator adds a custom route to a ViewSet without leaving the class.

from rest_framework.decorators import action

@action(detail=True, methods=['post'])
def favorite(self, request, pk=None):
    ...

Quick Check

Time to lock in how ViewSets and routers fit together.

Recap: CRUD in a Few Lines

You learned that a ModelViewSet plus a router turns two short classes into a complete CRUD API, with clean URLs generated for you. Nicely done! 🎉

Frequently asked questions

Is the “ModelViewSet and Routers” lesson free?

Yes — the full text of “ModelViewSet and Routers” is free to read here on the web, and the Django Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Django Academy course, upgrade to CoddyKit PRO.

What will I learn in “ModelViewSet and Routers”?

Generate CRUD routes automatically. You practise Django Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Django Academy?

No prior experience is required. Django Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “ModelViewSet and Routers” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Django Academy lesson?

Yes. Every Django Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. ModelViewSet and Routers
  2. Permissions and Throttling
  3. Token and JWT Authentication
  4. Filtering, Search, and Pagination
← Back to Django Academy