Author avatar

Kimaru Thagana

Develop a Microservice API with Django Rest Framework (DRF)

Kimaru Thagana

  • Oct 19, 2020
  • 7 Min read
  • Oct 19, 2020
  • 7 Min read
Web Development
Back End Web Development
Server-side Frameworks


Decoupling the development of an application into the frontend and the backend is a common architectural implementation in modern app development. With the frontend and backend decoupled, there needs to be a communication middleware that will allow data exchange between these components. This is where application programming interfaces (APIs) come in.

This guide explores API development in REST using Django, which already has a package for REST API development called Django Rest Framework (DRF). The guide assumes you have a basic understanding of APIs and Django web framework and at least intermediate-level proficiency in Python.

An introductory guide on Django can be found here.

About DRF

The Django Rest Framework is a third-party package that empowers a Django app with REST API capabilities.

To install the package, run the command:

1pip install django-rest-framework

Create a Sample App

This will be a Django app that displays data from the default Sqlite3 database. The data is a list of countries and their currency names.

Fire up a new Django project by running this command.

1django-admin startproject countries

Next, create an app and name it currency_country using this command.

1python3 startapp currency_country

The app is now set up. What remains is to develop the country model, the DRF API resources, and provide URLs to the API resources. The code block below shows the model and what fields make up the database table. Copy the code block into the file.

1from django.db import models
3class Country(models.Model):
4   country_name = models.CharField(max_length=20)
5   local_currency = models.CharField(max_length=20)
6   added_on = models.DateTimeField(auto_now_add=True)
8   def __str__(self):
9       return self.country_name

The model needs data. To add the data, access the admin panel that requires superuser access. The next step is to create a superuser account using the following command.

1python createsuperuser

This will be an interactive process where you will give essential details and register your user.

For the app and model to be visible, they need to be registered with the Django project and To register the app and rest framework, a third-party app, add their names to the list of installed apps in

Copy the list below to your and replace the existing list.

2    'django.contrib.admin',
3    'django.contrib.auth',
4    'django.contrib.contenttypes',
5    'django.contrib.sessions',
6    'django.contrib.messages',
7    'django.contrib.staticfiles',
8    'currency_country',
9    "rest_framework",

To ensure the Country model is visible on the admin panel, register it in the file.

Add the code block below to your file.

1from django.contrib import admin
2from .models import Country

DRF Serializers

DRF serializers convert Django data types, such as querysets, into a format that can be rendered into JSON or XML. For this app, you only need to create the Country serializer. In the currency_country app, create a file named and add the code block below.

1from rest_framework import serializers
2from .models import Country
5class CountrySerializer(serializers.ModelSerializer):
6    class Meta:
7        model = Country # this is the model that is being serialized
8        fields = ('country_name', 'local_currency')

API Requests

To service a GET or POST request, you need a view that will return all countries in a serialized fashion. To achieve this, create a view within the file and add the view that will return all countries, serialized.

1from rest_framework import status
2from rest_framework.decorators import api_view
3from rest_framework.response import Response
4from .models import Country
5from .serializers import CountrySerializer
8@api_view(['GET', 'POST'])
9def country(request):
11    if request.method == 'GET': # user requesting data 
12        snippets = Country.objects.all()
13        serializer = CountrySerializer(snippets, many=True)
14        return Response(
16    elif request.method == 'POST': # user posting data
17        serializer = CountrySerializer(
18        if serializer.is_valid():
19   # save to db
20            return Response(, status=status.HTTP_201_CREATED)
21        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

URL Configuration

To configure the URL, configure the main project's file to direct any traffic to the currency_country app using the path function, as in the codeblock below.

1from django.urls import path, include
2from django.contrib import admin
3urlpatterns = [
4    path('admin/',,
5    path('', include('currency_country.urls')),

Next, create a file and, using the path, direct the traffic to your view function.

1from django.urls import path
2from .views import country
5urlpatterns = [
6    path('country/', country, name="countries")

Run the App

When all has been set up, the final step is migrating the changes and running the server. To make migrations, run the command:

1python makemigrations

To migrate the model changes into the default database, run the command:

1python migrate

To start the server and run the app, run the command:

1python runserver

API Screen



Because Django is a popular web development framework and the current architecture of most applications is decoupled, API development in Django is a paramount skill. It can be applied in job positions such as backend developer, API/middleware developer, and full-stack developer.

To further build on your knowledge of Django Rest Framework, explore API concepts such as: