Chapter 16 · 10 min read
Next Steps: APIs, Tasks and Beyond
Next Steps: APIs, Tasks and Beyond
You have built a full Django project. This last chapter shows how to expose your data as a JSON API, what Django's built-in background tasks are for, and how to keep learning.
What you will learn
- How to build a small read-only JSON API with Django REST Framework.
- What the Django 6.0 Tasks framework is, and what it leaves to you.
- What changes in Django 6.2 LTS and later.
- A learning path after this course.
Django REST Framework
Django REST Framework (DRF) is the standard add-on for building JSON APIs on top of Django models. Its documentation lists support for Django 5.2, 6.0 and 6.1.
1. Install and register
python -m pip install djangorestframework
Add 'rest_framework' to INSTALLED_APPS in config/settings.py.
2. A serializer and a viewset
Create blog/api.py:
from rest_framework import routers, serializers, viewsets
from .models import Post
class PostSerializer(serializers.ModelSerializer):
author = serializers.ReadOnlyField(source="author.username")
class Meta:
model = Post
fields = ["id", "title", "body", "author", "created"]
class PostViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Post.objects.filter(published=True)
serializer_class = PostSerializer
router = routers.DefaultRouter()
router.register("posts", PostViewSet)
- A serializer converts model objects to and from JSON, much as a form does for HTML.
- A viewset bundles the list and detail behaviour.
ReadOnlyModelViewSetprovides only reading. - A router generates the URLs.
3. Route it
In config/urls.py add the import at the top and the path above the blog line:
from blog.api import router
path("api/", include(router.urls)),
Open http://127.0.0.1:8000/api/posts/ to see your published posts as JSON.
Background tasks in Django 6.0
Some work should not happen while a visitor waits: sending email, resizing images, building reports. Django 6.0 added a built-in Tasks framework for this. You declare a task with the task() decorator, and enqueue it through a configured backend. Django standardises how tasks are defined, validated, queued and how results are reported, but running them remains the job of external worker processes. Read the Tasks documentation before using them; they are a good topic for after this course.
What is coming
- Django 6.2 is the next long-term support release, planned for April 2027, and is the last release under the old version numbering.
- After that, releases are named by year: Django 2028 in January 2028. Every feature release will get the same three-year support period.
- Your knowledge carries over. When you upgrade, read the release notes and run your tests with warnings enabled to find deprecations.
A learning path
| Step | What to do |
|---|---|
| 1 | Redo the official tutorial (a polls app) to see the same ideas in another project |
| 2 | Add features to the blog: comments, categories, search |
| 3 | Learn PostgreSQL and deploy the blog to a real host |
| 4 | Build an API with DRF and consume it from a small JavaScript page |
| 5 | Learn caching, background tasks and logging |
| 6 | Read other people's Django code and the Django source |
Exercises with solutions
Exercise 1: write access
Allow logged-in users to create posts through the API.
Solution. Use ModelViewSet and require authentication:
from rest_framework import permissions
class PostViewSet(viewsets.ModelViewSet):
queryset = Post.objects.filter(published=True)
serializer_class = PostSerializer
permission_classes = [permissions.IsAuthenticatedOrReadOnly]
def perform_create(self, serializer):
serializer.save(author=self.request.user)
perform_create is the hook where you add the author, just like form_valid in a generic view. Because the serializer's author field is read-only, the client cannot choose it.
Exercise 2: restrict the fields
Remove body from the list output but keep it in the detail view.
Solution. Create a second, shorter serializer and override get_serializer_class() in the viewset to return it when self.action == "list".
Exercise 3: plan your project
Write down a project idea that is not a blog, and list its models.
Solution. There is no single right answer. For a to-do app, a sensible start is a Task model with title, done, due_date and a ForeignKey to the user, a ModelForm, list and update views, and tests that prove users cannot see each other's tasks.
Common mistakes
- Forgetting to add
'rest_framework'toINSTALLED_APPS. - Exposing private fields in a serializer. List fields explicitly, and avoid blindly using all fields.
- Jumping to APIs before the basics feel solid. Finish the capstone first.
- Treating background tasks as automatic. You still need worker processes.
Key takeaways
- DRF turns models into JSON APIs with serializers, viewsets and routers.
- Django 6.0's Tasks framework defines background tasks; separate workers run them.
- Django 6.2 LTS is planned for April 2027, and year-based versions begin with Django 2028.
- Keep learning by building and extending real projects.
Official links
- Django REST Framework quickstart: https://www.django-rest-framework.org/tutorial/quickstart/
- Tasks framework: https://docs.djangoproject.com/en/6.1/topics/tasks/
- Django 6.0 release notes: https://docs.djangoproject.com/en/6.1/releases/6.0/
- Django Forum: https://forum.djangoproject.com/
- MDN Django tutorial: https://developer.mozilla.org/en-US/docs/Learn_web_development/Extensions/Server-side/Django
