Django-rest Framework, in simple words

Let’s break down Django REST Framework (DRF) in simple terms. It’s a powerful tool to build web APIs using Python and the Django framework. Think of an API as a way for your app to talk to other apps or services—like a waiter taking orders from customers (frontend/apps) to the kitchen (your backend).
Here’s a beginner-friendly explanation:
What is Django REST Framework?
Django: A Python framework to build web apps quickly (handles databases, URLs, templates, etc.).
REST: A style of building APIs where you use simple web requests (like GET, POST) to get or send data.
DRF: An add-on to Django that makes creating RESTful APIs easier and faster.
With DRF, you can turn your Django app into something that sends and receives data (like JSON) instead of just showing HTML pages.
Key Concepts (in Simple Words)
Models: These are like blueprints for your data (e.g., a "User" model with name and email fields). You create them in Django.
Serializers: Think of them as translators. They convert your models (Python objects) into JSON (data that apps can understand) and back.
Views: These decide what happens when someone visits your API (e.g., "show all users" or "add a new user").
URLs: These are the addresses (like /api/users/) where your API lives.
How to Use DRF: A Simple Example
Let’s say you’re building an API for a to-do list app.
Step 1: Set Up
Install Django and DRF:
bash
pip install django djangorestframework
Start a Django project:
bash
django-admin startproject todo_project cd todo_project python manage.py startapp todos
Add rest_framework and todos to INSTALLED_APPS in todo_project/settings.py.
Step 2: Create a Model
In todos/models.py, define a "Task":
python
from django.db import models
class Task(models.Model):
title = models.CharField(max_length=100)
completed = models.BooleanField(default=False)
def __str__(self):
return self.title
Run these to create the database:
bash
python manage.py makemigrations
python manage.py migrate
Step 3: Create a Serializer
In todos/serializers.py, write:
python
from rest_framework import serializers
from .models import Task
class TaskSerializer(serializers.ModelSerializer):
class Meta:
model = Task
fields = ['id', 'title', 'completed'] # Fields to include in the API
This turns your Task model into JSON.
Step 4: Create a View
In todos/views.py, write:
python
from rest_framework import generics
from .models import Task
from .serializers import TaskSerializer
class TaskList(generics.ListCreateAPIView): # List all tasks or create a new one
queryset = Task.objects.all()
serializer_class = TaskSerializer
class TaskDetail(generics.RetrieveUpdateDestroyAPIView): # Get, update, or delete a task
queryset = Task.objects.all()
serializer_class = TaskSerializer
ListCreateAPIView: Handles GET (list all) and POST (create).
RetrieveUpdateDestroyAPIView: Handles GET (one task), PUT (update), DELETE.
Step 5: Set Up URLs
In todos/urls.py, write:
python
from django.urls import path
from .views import TaskList, TaskDetail
urlpatterns = [
path('tasks/', TaskList.as_view(), name='task-list'),
path('tasks/<int:pk>/', TaskDetail.as_view(), name='task-detail'),
]
Then link it in todo_project/urls.py:
python
from django.urls import path, include
urlpatterns = [
path('api/', include('todos.urls')),
]
Step 6: Test It
Run the server:
bash
python manage.py runserver
Go to http://127.0.0.1:8000/api/tasks/ in your browser or use a tool like Postman.
You can GET a list of tasks or POST a new one like {"title": "Buy milk", "completed": false}.
What You Get
GET /api/tasks/: See all tasks (e.g., [{"id": 1, "title": "Buy milk", "completed": false}]).
POST /api/tasks/: Add a new task.
GET /api/tasks/1/: See details of task with ID 1.
PUT /api/tasks/1/: Update task 1.
DELETE /api/tasks/1/: Delete task 1.
Why DRF is Cool
It does the hard work for you (like handling requests and responses).
Built-in tools like authentication and permissions (e.g., only logged-in users can add tasks).
Browsable API: DRF gives you a simple web interface to test your API.
Next Steps
Play with the example above.
Add authentication (DRF has easy ways to do this).
Try more complex models or relationships (e.g., tasks with categories).
Let me know if you want to dive deeper into any part!
Subscribe to my newsletter
Read articles from Singaraju Saiteja directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Singaraju Saiteja
Singaraju Saiteja
I am an aspiring mobile developer, with current skill being in flutter.