Module 10 / Project 04 — REST Framework¶
Home: README
Learn Your Way¶
| Read | Build | Watch | Test | Review | Visualize | Try |
|---|---|---|---|---|---|---|
| — | This project | — | — | Flashcards | — | — |
Focus¶
Django REST Framework (DRF) — serializers, viewsets, routers, and the browsable API.
Why this project exists¶
Django's template system renders HTML for browsers, but modern applications often need a JSON API for mobile apps, single-page applications, or third-party integrations. Django REST Framework adds this capability to Django. If you used FastAPI in Module 04, DRF is Django's equivalent: it provides serializers (like Pydantic models), viewsets (like route handlers), and routers (like FastAPI's path decorators).
DRF also includes a browsable API — a web interface where you can interact with your API directly in the browser, similar to FastAPI's /docs page.
Run¶
cd projects/modules/10-django-fullstack/04-rest-framework
python manage.py migrate
python manage.py createsuperuser
python manage.py runserver
Then open your browser to:
- http://127.0.0.1:8000/api/ — API root (browsable API)
- http://127.0.0.1:8000/api/books/ — list all books (JSON)
- http://127.0.0.1:8000/api/authors/ — list all authors (JSON)
- http://127.0.0.1:8000/admin/ — add books and authors via admin
Press Ctrl+C to stop the server.
Expected output¶
Visiting /api/books/ in a browser shows DRF's browsable API with a list of books in JSON format. You can create new books using the form at the bottom of the page. Visiting /api/books/1/ shows details for a single book.
Visiting /api/books/?format=json returns raw JSON:
[
{
"id": 1,
"title": "Django for Beginners",
"author": 1,
"author_name": "William Vincent",
"price": "39.99",
"published_date": "2024-01-15"
}
]
Alter it¶
- Add a search filter: install
django-filterand addfilterset_fields = ["author"]toBookViewSet. Users can then filter books by author:/api/books/?author=1. - Add pagination: in
settings.py, addREST_FRAMEWORK = {"DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.PageNumberPagination", "PAGE_SIZE": 10}. - Add a custom action to
BookViewSetthat returns only books under $20: use@action(detail=False)decorator.
Break it¶
- In
BookSerializer, removeauthor_namefromfields. Visit/api/books/. How does the response change? - In
urls.py, comment outrouter.register("books", ...). Visit/api/books/. What error do you get? - Remove
"rest_framework"fromINSTALLED_APPS. Try running the server. What happens?
Fix it¶
- Add
author_nameback.SerializerMethodFieldadds computed (read-only) fields to the API response. Removing it fromfieldsremoves it from the output. - Uncomment the router registration. DRF routers generate URL patterns automatically from viewsets. Without registration, the URL pattern does not exist.
- Add
"rest_framework"back. DRF must be inINSTALLED_APPSbecause it includes templates (for the browsable API), static files, and app configuration.
Explain it¶
- What is a serializer? How does it compare to Pydantic models in FastAPI?
- What is the difference between a ViewSet and a regular Django view?
- What does a DRF router do? How does it generate URL patterns from viewsets?
- What is the browsable API? Why is it useful during development?
Mastery check¶
You can move on when you can:
- create a serializer for any model with both model fields and computed fields,
- create a ViewSet with full CRUD operations,
- wire viewsets to URLs using a router,
- use the browsable API to test all CRUD operations.
Related Concepts¶
Next¶
Continue to 05-complete-app.