codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐍

Python Programming

63 / 68 topics
59Web Development with Flask60Django Framework Basics61Django ORM & Models62Django Templates & Jinja263Building REST APIs with Django Rest Framework (DRF)
Tutorials/Python Programming/Building REST APIs with Django Rest Framework (DRF)
🐍Python Programming

Building REST APIs with Django Rest Framework (DRF)

Updated 2026-05-15
45 min read

Building REST APIs with Django Rest Framework (DRF)

In the world of web development, APIs are essential for enabling communication between different software systems. Django Rest Framework (DRF) is a powerful and flexible toolkit that simplifies the process of building Web APIs in Django applications. This tutorial will guide you through setting up DRF, creating serializers, using ViewSets and routers, implementing authentication and permissions, and building a complete CRUD REST API.

Introduction

APIs are the backbone of modern web applications, allowing different components to interact seamlessly. Django Rest Framework (DRF) is one of the most popular libraries for building REST APIs in Python. It provides a comprehensive set of tools that simplify the development process, making it easier to create robust and scalable APIs.

In this tutorial, you'll learn how to set up DRF, define serializers, use ViewSets and routers, implement authentication and permissions, and build a complete CRUD (Create, Read, Update, Delete) REST API. By the end of this tutorial, you'll have a solid understanding of how to leverage DRF to create efficient and secure APIs.

Core Content

1. Setting Up Django Rest Framework

Before you can start using DRF, you need to install it in your Django project.

Terminal

You can now access your CRUD API at http://localhost:8000/books/.

Practical Example

Let's build a complete example of a REST API for managing books.

Step 1: Create the Django Project and App

First, create a new Django project and app.

Terminal
$ django-admin startproject myproject
$ cd myproject
$ python manage.py startapp books

Add 'books' to INSTALLED_APPS in settings.py.

Step 2: Define the Model

Define the Book model in books/models.py.

books/models.py
1from django.db import models
2
3class Book(models.Model):
4 title = models.CharField(max_length=100)
5 author = models.CharField(max_length=100)
6 published_date = models.DateField()
7
8 def __str__(self):
9 return self.title

Step 3: Create the Serializer

Create a serializer for the Book model in books/serializers.py.

books/serializers.py
1from rest_framework import serializers
2from .models import Book
3
4class BookSerializer(serializers.ModelSerializer):
5 class Meta:
6 model = Book
7 fields = ['id', 'title', 'author', 'published_date']

Step 4: Define the ViewSet

Define a ViewSet for the Book model in books/views.py.

books/views.py
1from rest_framework import viewsets
2from .models import Book
3from .serializers import BookSerializer
4
5class BookViewSet(viewsets.ModelViewSet):
6 queryset = Book.objects.all()
7 serializer_class = BookSerializer

Step 5: Set Up the Router

Set up a router in books/urls.py.

books/urls.py
1from django.urls import path, include
2from rest_framework.routers import DefaultRouter
3from .views import BookViewSet
4
5router = DefaultRouter()
6router.register(r'books', BookViewSet)
7
8urlpatterns = [
9 path('', include(router.urls)),
10]

Step 6: Include the App URLs in the Project

Include the app URLs in myproject/urls.py.

myproject/urls.py
1from django.contrib import admin
2from django.urls import path, include
3
4urlpatterns = [
5 path('admin/', admin.site.urls),
6 path('', include('books.urls')),
7]

Step 7: Apply Migrations and Run the Server

Apply migrations and run the Django development server.

Terminal
$ python manage.py makemigrations
$ python manage.py migrate
$ python manage.py runserver

You can now access your CRUD API at http://localhost:8000/books/.

Summary

  • DRF Setup: Install DRF and add it to INSTALLED_APPS.
  • Serializers: Define serializers to convert complex data types into Python datatypes.
  • ViewSets and Routers: Use ViewSets to combine related views and routers to automatically generate URL confs.
  • Authentication and Permissions: Implement authentication and permissions to control access to your API endpoints.
  • Building a CRUD REST API: Create a complete CRUD API for managing books.

What's Next?

Congratulations on completing this tutorial on building REST APIs with Django Rest Framework! You now have a solid foundation in creating powerful and secure APIs. In the next topic, we'll dive into data structures such as stacks, queues, and linked lists, which are essential for many programming applications. Stay tuned!


PreviousDjango Templates & Jinja2Next Stacks, Queues & Linked Lists

Recommended Gear

Django Templates & Jinja2Stacks, Queues & Linked Lists