How to build an API with Django REST Framework within 30 minutes

In this blog post, I will introduce how to develop a API Service in Django using Django REST Framework (DRF). In this example you will be introduced to DRF with minimal amount of code.

Let’s start

In this tutorial, you will be building a simple API for your inventory service.

Setup your development environment

For this tutorial, I’m using python 3.8.1 that is installed on my Apple. If you are running Linux or Windows that’s not an problem. To check you local version of Python you can use the following command.

mark@Xenophobia: python3 -V
Python 3.8.1

To start we will create a virtual environment with virtualenv and install Django 2.0. A virtual environment within python create a private workspace that will not conflicting with your host installed python packages.

$ mkdir inventoryAPI
$ cd inventoryAPI
$ virtualenv -p python3 venv 
Running virtualenv with interpreter /usr/local/opt/python@3.8/bin/python3
Using base prefix '/usr/local/Cellar/python@3.8/3.8.1/Frameworks/Python.framework/Versions/3.8'
New python executable in /Users/mark/Documents/Development/venv/bin/python3.8
Also creating executable in /Users/mark/Documents/Development/venv/bin/python
Installing setuptools, pip, wheel…
done.

$ source venv/bin/activate
(venv) $

A little recap of above. We have created a project directory called inventoryAPI. And we accessed the directory. In this directory we created a virtual environment and activated the enviroment.

Next we are installing the django framework and the django REST framework with the pip command.

(venv) $ pip install Django==2.1.5
Collecting Django==2.1.5
Downloading Django-2.1.5-py3-none-any.whl (7.1 MB)
|████████████████████████████████| 7.1 MB 10.0 MB/s
Collecting pytz
Using cached pytz-2020.1-py2.py3-none-any.whl (510 kB)
Installing collected packages: pytz, Django
Successfully installed Django-2.1.5 pytz-2020.1
(venv) $

(venv) $ pip install djangorestframework
Collecting djangorestframework
Using cached djangorestframework-3.11.0-py3-none-any.whl (911 kB)
Requirement already satisfied: django>=1.11 in ./venv/lib/python3.8/site-packages (from djangorestframework) (2.0.3)
Requirement already satisfied: pytz in ./venv/lib/python3.8/site-packages (from django>=1.11->djangorestframework) (2020.1)
Installing collected packages: djangorestframework
Successfully installed djangorestframework-3.11.0
(venv) $

To start with building applications with Djanogo we first need to create a application. In our example we create the inventory application within the Django framework.

$ django-admin startproject api .
$ django-admin startapp inventory

With the ls in your current directory you will see the base files for our Django project.

(venv) $ ls
api inventory manage.py venv
(venv) $

Next is to setup/migrate the database and create a administrative user.

(venv) $ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial… OK
Applying auth.0001_initial… OK
Applying admin.0001_initial… OK
Applying admin.0002_logentry_remove_auto_add… OK
Applying contenttypes.0002_remove_content_type_name… OK
Applying auth.0002_alter_permission_name_max_length… OK
Applying auth.0003_alter_user_email_max_length… OK
Applying auth.0004_alter_user_username_opts… OK
Applying auth.0005_alter_user_last_login_null… OK
Applying auth.0006_require_contenttypes_0002… OK
Applying auth.0007_alter_validators_add_error_messages… OK
Applying auth.0008_alter_user_username_max_length… OK
Applying auth.0009_alter_user_last_name_max_length… OK
Applying sessions.0001_initial… OK

(venv) bash-3.2$ python manage.py createsuperuser --email admin@example.com --username admin
Password:
Password (again):
Superuser created successfully.
(venv) $

Next step is to change the api/settings.py file and add the two applications to the INSTALLED_APPS section.

INSTALLED_APPS = [
    ...
    'rest_framework',
    'inventory'
 ]

Next step is to change the api/url.py file and add the urls for the inventory app.

"""
from django.contrib import admin
from django.urls import path

urlpatterns = [
path('admin/', admin.site.urls),
path('api/', inventory.urls),
]

In default the SQlite database has been chosen. Optional you can migrate this to mysql or postgresql or any other RDBMS you prefer.

Next Step is to create your View for the inventory application. We start with creating your model. There you define how your inventory objects look like. You need to edit the inventory/models.py

from django.db import models

class Devices(models.Model): 
    name = models.CharField(max_length=255, null=False) 
    owner = models.CharField(max_length=255, null=False)
    def __str__(self): 
        return "{} - {}".format(self.name, self.owner)

Due to we are using Django, we can also add our model to the admin pannel where you can enter data or change data with use of an form. To add our models to the admin page you need to register your model. You can change the inventory/admin.py file to register the devices model we created above.

from django.contrib import admin

from .models import Devices
admin.site.register(Devices)

Now we can perform the database migration to create the actual database in sqlLite. We do this with the makemigrations and migrate command.

(venv) $ python manage.py makemigrations
(venv) $ python manage.py migrate

After the database migrations. We can start with creating the serializers. Serializers will help you to allow complex data structures such as querysets and models instances to be converted to native Python datatypes that can then be easily rendered into for example JSON or XML.

For this we need to create a new file in our project inventory/serializers.py and add the following lines of code.

from rest_framework import serializers
from .models import Devices


class DevicesSerializer(serializers.ModelSerializer):
    class Meta:
        model = Devices
        fields = ("name", "owner")

Finally we need to create our view that returns all of our devices. Open the file inventory/views.py and add the following lines of code.

from rest_framework import generics
from .models import Devices
from .serializers import DevicesSerializer


class ListDevicesView(generics.ListAPIView):
    """
    Provides a get method handler.
    """
    queryset = Devices.objects.all()
    serializer_class = DevicesSerializer

We now need to connect our views with URL routes. We need the change the general URL routes towards our inventory URLS and the inventory URLS to our specific model list.

You start with the api/urls.py file and add the following lines of code.

 """api URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, re_path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    #path('api/', inventory.urls),
    re_path('api/(?P<version>(v1|v2))/', include('inventory.urls'))
]

And for the inventory/urls.py you add the following lines of code

 from django.urls import path
from .views import ListDevicesView

urlpatterns = [
    path('devices/', ListDevicesView.as_view(), name="devices-all")
]

You are now ready for your first test.

(venv) $ python manage.py test

and the response will be

Creating test database for alias 'default'…
System check identified no issues (0 silenced).
.
Ran 1 test in 0.011s
OK
Destroying test database for alias 'default'…
(venv) bash-3.2$

Now you can start your application for the first time

(venv) $ python manage.py runserver

And you will see

Performing system checks…
System check identified no issues (0 silenced).
July 03, 2020 - 11:21:35
Django version 2.1.5, using settings 'api.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Now you can browse to the following url: http://127.0.0.1:8000/admin

Now you can browse to the following url: http://127.0.0.1:8000/admin

Now you have a fully working API which you can use as endpoint for your application. I recommend to read the official DRF and Django documentation for future steps in your API.

Share the Post:

Related Posts