Django REST Framework is a wrapper over the default Django Framework, basically used to create APIs of various kinds. There are three stages before creating an API through the REST framework, Converting a Model’s data to JSON/XML format (Serialization), Rendering this data to the view, and Creating a URL for mapping to the views.
This article revolves around how to create a basic API using the Django REST Framework. It assumes you are familiar with Django basics – Django tutorial—also, installation of Django REST Framework. Assuming you have created a project named geeksforgeeks with Django, let’s initiate Django REST Framework.
Steps
- Add rest_framework to INSTALLED_APPS
- Create a app and model
- Serialization
- Creating a viewset
- Define URLs of API
- Run server and check API
Creating API in Django using Django Rest Framework
Add rest_framework to INSTALLED_APPS
To initialize REST Framework in your project, go to settings.py, and in INSTALLED_APPS add ‘rest_framework’ at the bottom to create api in python django.
Python3
INSTALLED_APPS = [
'django.contrib.admin' ,
'django.contrib.auth' ,
'django.contrib.contenttypes' ,
'django.contrib.sessions' ,
'django.contrib.messages' ,
'django.contrib.staticfiles' ,
'rest_framework' ,
]
|
Create a app and model
Now, let’s create a app using command,
python manage.py startapp apis
A folder with name apis would have been registered by now. let’s add this app to INSTALLED_APPS and urls.py also.
In, settings.py.
Python3
INSTALLED_APPS = [
'django.contrib.admin' ,
'django.contrib.auth' ,
'django.contrib.contenttypes' ,
'django.contrib.sessions' ,
'django.contrib.messages' ,
'django.contrib.staticfiles' ,
'rest_framework' ,
'apis' ,
]
|
Now, add APIs urls in urls.py. In geeksforgeeks.urls.py.
Python3
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path( 'admin/' , admin.site.urls),
path('', include( "apis.urls" ))
]
|
Create a model
To demonstrate, create api in python django and using an API, let’s create a model named “GeeksModel”. In apis/models.py
Python3
from django.db import models
class GeeksModel(models.Model):
title = models.CharField(max_length = 200 )
description = models.TextField()
def __str__( self ):
return self .title
|
now our app is ready, let’s serialize the data and create views from the same.
Serializing Django objects
Serializers allow complex data such as querysets and model instances to be converted to native Python datatypes that can then be easily rendered into JSON, XML or other content types. Serializers also provide deserialization, allowing parsed data to be converted back into complex types, after first validating the incoming data. Let’s start creating a serializer, in file apis/serializers.py,
Python3
from rest_framework import serializers
from .models import GeeksModel
class GeeksSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = GeeksModel
fields = ( 'title' , 'description' )
|
Creating a viewset
To render data into frontend, and handle requests from user, we need to create a view. In Django REST Framework, we call these as viewsets, so let’s create a view in apis/views.py,
Python3
from rest_framework import viewsets
from .serializers import GeeksSerializer
from .models import GeeksModel
class GeeksViewSet(viewsets.ModelViewSet):
queryset = GeeksModel.objects. all ()
serializer_class = GeeksSerializer
|
Define URLs of API
Now create api in python django and Specify the url path of APIs to be accessed, In apis/urls.py,
Python3
from django.urls import include, path
from rest_framework import routers
from .views import *
router = routers.DefaultRouter()
router.register(r 'geeks' , GeeksViewSet)
urlpatterns = [
path('', include(router.urls)),
path( 'api-auth/' , include( 'rest_framework.urls' ))
]
|
After everything is successfully ready, let’s run some commands to activate the server.
Run server and check API
Run following commands to create the database, and then run server,
python manage.py makemigrations
python manage.py migrate
python manage.py runserver
Now visit http://127.0.0.1:8000/geeks/,

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
01 Sep, 2023
Like Article
Save Article