Author avatar

Kimaru Thagana

Develop a Microservice API with Django Rest Framework (DRF)

Kimaru Thagana

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

Introduction

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
bash

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
bash

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

1python3 manage.py startapp currency_country
python

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 models.py file.

1from django.db import models
2
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)
7   
8   def __str__(self):
9       return self.country_name
python

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 manage.py createsuperuser
python

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 admin.py. To register the app and rest framework, a third-party app, add their names to the list of installed apps in settings.py.

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

1INSTALLED_APPS = [
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",
10]
python

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

Add the code block below to your admin.py file.

1from django.contrib import admin
2from .models import Country
3admin.site.register(Country)
python

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 serializers.py and add the code block below.

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

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 views.py 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
6
7
8@api_view(['GET', 'POST'])
9def country(request):
10    
11    if request.method == 'GET': # user requesting data 
12        snippets = Country.objects.all()
13        serializer = CountrySerializer(snippets, many=True)
14        return Response(serializer.data)
15
16    elif request.method == 'POST': # user posting data
17        serializer = CountrySerializer(data=request.data)
18        if serializer.is_valid():
19            serializer.save() # save to db
20            return Response(serializer.data, status=status.HTTP_201_CREATED)
21        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
python

URL Configuration

To configure the URL, configure the main project's urls.py 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/', admin.site.urls),
5    path('', include('currency_country.urls')),
6
7]
python

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

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

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 manage.py makemigrations
bash

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

1python manage.py migrate
bash

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

1python manage.py runserver
bash

API Screen

DRFDRF

Conclusion

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: