Auth, APIs & ProjectPro· 35 min read

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:

Install DRF
pip install djangorestframework

This 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:

A serializer turns the model into JSON
# 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:

A viewset gives you full CRUD API endpoints
# 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 = PostSerializer

queryset 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:

  1. A client (a React app, a phone, or even your browser) requests an endpoint such as /api/posts/.
  2. The viewset runs its queryset to fetch the matching posts from the database.
  3. The serializer converts those Post objects into JSON text.
  4. DRF sends that JSON back as the response.
  5. 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?

Answer: A serializer converts model instances to/from JSON for the API.

✍️ Practice

  1. Install DRF and create a serializer for a model.
  2. Expose it with a viewset and view the JSON in the browser.

🏠 Homework

  1. Build a small JSON API for one model and test it.
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →