Django REST Framework

Django REST Framework (DRF) is a powerful toolkit built on top of Django for building Web APIs.

It allows you to create RESTful APIs easily and efficiently using Django models.

If Django is used for building websites, DRF is used for building APIs for:

  • Mobile applications
  • Single Page Applications (React, Angular, Vue)
  • Third-party integrations
  • Microservices

Why Use Django REST Framework?

DRF provides:

  • Easy serialization of data
  • Authentication and permissions
  • Browsable API interface
  • Built-in support for JSON
  • Powerful request handling
  • Viewsets and routers
  • Token authentication

It saves a lot of development time.

Installing Django REST Framework

pip install djangorestframework

Add it to settings.py:

INSTALLED_APPS = [
...
'rest_framework',
]

What is Serialization?

Serialization converts complex data (like Django models) into JSON format.

Example Model:

from django.db import modelsclass Product(models.Model):
name = models.CharField(max_length=100)
price = models.IntegerField()

Create a Serializer:

from rest_framework import serializers
from .models import Productclass ProductSerializer(serializers.ModelSerializer):
class Meta:
model = Product
fields = '__all__'

Now the model can be converted to JSON.

Creating an API View

Example API view:

from rest_framework.decorators import api_view
from rest_framework.response import Response
from .models import Product
from .serializers import ProductSerializer@api_view(['GET'])
def product_list(request):
products = Product.objects.all()
serializer = ProductSerializer(products, many=True)
return Response(serializer.data)

This returns data in JSON format.

Example JSON Response

[
{
"id": 1,
"name": "Laptop",
"price": 80000
},
{
"id": 2,
"name": "Mobile",
"price": 30000
}
]

Using ViewSets (Cleaner Approach)

DRF provides ViewSets to reduce repetitive code.

from rest_framework import viewsets
from .models import Product
from .serializers import ProductSerializerclass ProductViewSet(viewsets.ModelViewSet):
queryset = Product.objects.all()
serializer_class = ProductSerializer

Using Routers

In urls.py:

from rest_framework.routers import DefaultRouter
from .views import ProductViewSetrouter = DefaultRouter()
router.register(r'products', ProductViewSet)urlpatterns = router.urls

Now DRF automatically creates:

GET /products
POST /products
GET /products/1
PUT /products/1
DELETE /products/1

Authentication in DRF

DRF supports:

  • Session Authentication
  • Token Authentication
  • JWT Authentication

Example setting:

REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.TokenAuthentication',
],
}

Permissions

You can restrict API access:

from rest_framework.permissions import IsAuthenticated
from rest_framework.decorators import api_view, permission_classes@api_view(['GET'])
@permission_classes([IsAuthenticated])
def secure_view(request):
return Response({"message": "Authenticated User"})

Browsable API

One powerful feature of DRF is its built-in web interface.

When you open the API URL in a browser, you can:

  • View data
  • Send POST requests
  • Update data
  • Delete data

This is very helpful during development.

Why Learn Django REST Framework?

DRF helps you:

Build APIs quickly
Connect backend with mobile apps
Create scalable backend systems
Develop modern web applications
Build microservices

Key Takeaway

Django REST Framework extends Django to build powerful RESTful APIs.

It simplifies serialization, authentication, permissions, and routing — making API development faster, cleaner, and more scalable.

Home » PYTHON FOR WEB DEVELOPMENT (PYWEB) > REST API Development > Django REST Framework