Learn the Django REST Framework in 5 Minutes

Gustav Willig
3 min readAug 6, 2022

Writing REST APIs is really commonplace for developers these days. When faced with the choice of technology stack, I tend to think of Django as one of my first options.

Let’s get started!

Logo of the Django Rest Framework (Source)

What is a REST API?

A REST API is a popular way for systems to display useful functions and data. REST, which stands for representational state transfer, can be made up of one or more resources that can be accessed at a given URL and returned in a variety formats, like JSON, images, HTML, and more.

Why Django REST framework?

Django REST framework (DRF) is a powerful and flexible toolkit for building Web APIs.Its primary advantage is that it makes serialazion of model data some much easier.

If you are familiar with Django, the Django REST framework is a great choice because it is built on the class-based views of Django. It incorporates implementations such as forms, class-based views, model validator, QuerySet etc.

Requirements

REST framework requires the following:

  • Python (3.6, 3.7, 3.8, 3.9, 3.10)
  • Django (2.2, 3.0, 3.1, 3.2, 4.0)

The framework creators highly recommend to use only latest patch release of each Python and Django series.

The following packages are optional:

Installation

Install using pip, including any optional packages you want...

pip install djangorestframework
pip install markdown # Markdown support for the browsable API.
pip install django-filter # Filtering support

…or clone the project from github.

git clone https://github.com/encode/django-rest-framework

Add 'rest_framework' to your INSTALLED_APPS setting.

INSTALLED_APPS = [
...
'rest_framework',
]

If you’re intending to use the browsable API you’ll probably also want to add REST framework’s login and logout views. Add the following to your root urls.py file.

urlpatterns = [
...
path('api-auth/', include('rest_framework.urls'))
]

Note that the URL path can be whatever you want.

Example

Let’s take a look at a quick example of using REST framework to build a simple model-backed API.

We’ll create a read-write API for accessing information on the users of our project.

Any global settings for a REST framework API are kept in a single configuration dictionary named REST_FRAMEWORK. Start off by adding the following to your settings.py module:

REST_FRAMEWORK = {
# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for unauthenticated users.
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
]
}

Don’t forget to make sure you’ve also added rest_framework to your INSTALLED_APPS.

We’re ready to create our API now. Here’s our project’s root urls.py module:

from django.urls import path, include
from django.contrib.auth.models import User
from rest_framework import routers, serializers, viewsets
# Serializers define the API representation.
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ['url', 'username', 'email', 'is_staff']
# ViewSets define the view behavior.
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
# Routers provide an easy way of automatically determining the URL conf.
router = routers.DefaultRouter()
router.register(r'users', UserViewSet)
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
path('', include(router.urls)),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]

Now, this should be the last piece of our API.

You can now open the API in your browser at http://127.0.0.1:8000/, and view your new ‘users’ API. If you use the login control in the top right corner you’ll also be able to add, create and delete users from the system.

Another tutorial can be found here.

--

--

Gustav Willig

An AI Full-Stack Developer with a passion for using data to drive business decisions. Get your latest news about Django and AI trends by subscribing