Create REST API using Django Rest Framework

  779 views   1 year ago Python

There are multiple ways to indite an API in Django, but I don't optate you confound them. I will show you the two best ways to implement APIs, which will cover virtually all the scenarios.

Both ways are homogeneous to each other. They are:

  • APIView
  • ModelViewSet

APIView is more generic, while ModelViewSet is categorical to a Model's CRUD operations (i.e. ModelViewSet is utilized to perform List, Engender, Read, Update, Expunge (CRUD ) operations only.) Whereas with APIView, you can implement not only CRUD operations but additionally other Non-CRUD functionalities like authenticate, logout, engender invoice, download invoice, change fields of multiple models, etc.

You must be cerebrating, "with APIView we can implement so much, why to learn ModelViewSet?" Well, you will decipher the reason very anon after implementing them both.

Let's commence with APIView first.


Create a Django project and application using the following steps.

# Install django in your system from command prompt
pip install django

# Create Project
django-admin startproject DemoProject

# Create Application inside the Project
cd DemoProject
django-admin startapp DemoApplication

# Add DemoApplication in > INSTALL_APPS list

Install Django Rest Framework

# Install django rest framework from command prompt
install djangorestframework
# Add rest_framework application in > INSTALLED_APPS list

Now, we are ready with the Django Rest Framework setup. Let's create our first API using APIView.


With APIView, we can implement Get, Post, Delete, Patch, Put methods. Let's create a Book data model and implement CRUD functionality on it.

Edit the file -  DemoProject > DemoApplication > and create a model. (Django will create a new SQL table in Database.)

class Book(models.Model):

Edit the file - DemoProject > DemoApplication >

from rest_framework.views import APIView
from rest_framework.response import Response
from .models import *
from .serializers import *
from rest_framework import viewsets

# Create your views here.
class BookApiView(APIView):
    def get(self,request):
        return Response({"Message":"List of Books", "Book List":allBooks})

    def post(self,request):
        return Response({"Message":"New Book Added!", "Book":book})

Edit the file - DemoProject >

from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from ApiApplication import views

urlpatterns = [
    url("book/",views.BookApiView.as_view()), #new

Now, Migrate and run the project.

python makemigrations
python migrate
python runserver

Open from your browser.

Voila! Your first API has created. 

Now Imagine, We have 10 data models (tables in database) and we have to create 10 such APIS and also implement CRUD functionality for the same. Isn't that a redundant task?

 To avoid repetitive code, Django has ModelViewSet where you can save 1000 of such lines.


Edit the file -  DemoProject > DemoApplication > and create a new data model.

class Student(models.Model):

Create a new file  -  DemoProject > DemoApplication >

from rest_framework import serializers
from .models import *

class StudentSerializer(serializers.ModelSerializer):
    class Meta:
        fields = "__all__"

Edit the file -  DemoProject > DemoApplication >

class StudentViewSet(viewsets.ModelViewSet):
    queryset = Student.objects.all()

Edit the file - DemoProject >

from django.conf.urls import url
from ApiApplication import views
from rest_framework import routers
from django.urls import include

router = routers.DefaultRouter()

urlpatterns = [

Now run the server and open from your browser.

i hope you like this article.

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]