Building an API (DRF)
Serve JSON with Django REST Framework — so a React or mobile app can use your data.
What you will learn
- Understand REST APIs in Django
- Create a serializer and API view
- Return JSON
Django REST Framework
DRF (short for Django REST Framework) is the standard tool for building APIs in Django. An API (Application Programming Interface — a way for programs to talk to each other) returns raw data instead of HTML pages, so other programs — a React app, a phone app — can use it. The data is usually sent as JSON (JavaScript Object Notation — a simple text format for data that looks like {"title": "Hello"}, easy for any program to read). REST is just a popular, predictable style for designing those web addresses (for example, /api/posts/ for the list of posts). Two pieces do the work: a serializer turns your models into JSON, and a viewset exposes them as web addresses (called endpoints — the URLs a program calls to get or send data).
DRF is a separate package, so first install it with pip:
pip install djangorestframeworkThis downloads DRF into your project. One more setup step: add "rest_framework" to the INSTALLED_APPS list in settings.py (just like you did for your own app), so Django knows it is there. Now you can use it.
A model is a Python object, but the internet speaks JSON. A serializer is the translator between the two. This one is built straight from the Post model:
# serializers.py
from rest_framework import serializers
from .models import Post
class PostSerializer(serializers.ModelSerializer):
class Meta:
model = Post
fields = "__all__"Because it extends ModelSerializer, DRF reads the model for you. The inner Meta class names which model to use (model = Post) and which fields to include (fields = "__all__" means every field). The result: each Post can be converted to JSON, and incoming JSON can be turned back into a Post.
A viewset ties the serializer to real URLs and gives you the full set of CRUD endpoints automatically:
# views.py
from rest_framework import viewsets
from .models import Post
from .serializers import PostSerializer
class PostViewSet(viewsets.ModelViewSet):
queryset = Post.objects.all()
serializer_class = PostSerializerqueryset tells the viewset which records to serve (all posts), and serializer_class tells it how to convert them to JSON. From just these two lines, ModelViewSet builds list, create, read, update and delete endpoints for you.
How a request becomes JSON, step by step:
- A client (a React app, a phone, or even your browser) requests an endpoint such as
/api/posts/. - The viewset runs its
querysetto fetch the matching posts from the database. - The serializer converts those
Postobjects into JSON text. - DRF sends that JSON back as the response.
- The client reads the JSON and shows it however it likes — no HTML involved.
Note: Output (JSON returned from the API):
[
{"id": 1, "title": "Hello", "body": "First post", "created": "2026-06-07T10:00:00Z"}
]
The serializer turned the Post rows into a JSON list any program can read.
Note: This closes the loop with everything you’ve learned: a React front-end could fetch from this Django API, just like it fetched from the Node/Express API in MERN.
Q. In Django REST Framework, what turns a model into JSON?
✍️ Practice
- Install DRF and create a serializer for a model.
- Expose it with a viewset and view the JSON in the browser.
🏠 Homework
- Build a small JSON API for one model and test it.