The Django REST framework (DRF) is a powerful, opinionated, yet flexible toolkit for building Web APIs. It builds on the rapid, clean, and pragmatic development design encouraged by Django.
DRF has some advantages that make it stands out among other REST frameworks:
Out of the box, it comes with both OAuth
,
$ django-admin startproject my_django_project
$ cd my_api_project
a and OAuth
,
$ django-admin startapp my_rest_api
authentication packages.
Support for both ORM and non-ORM data serialization.
Fully customizable to meet your organization’s needs.
Extensive documentation.
Great community support.
Follow these
,
from django.db import models
class Clubs(models.Model):
name = models.CharField(max_length=100)
classification = models.CharField(max_length=100)
country = models.CharField(max_length=100)
class Player(models.Model):
name = models.CharField(max_length=100)
birth_year = models.CharField(max_length=10)
eye_color = models.CharField(max_length=10)
clubs = models.ForeignKey(Clubs, on_delete=models.DO_NOTHING)
steps to set up your first Django API:
Set up Django and DRF.
Create your Django Models.
Set up DRF Serializers.
Set up URI endpoints to view your data.
,
$ django-admin startapp my_rest_api
style=”text-align: center;” align=”center”>Set up Django and DRF,
$ django-admin startapp my_rest_api
>
Prerequisites: You should have Python
,
$ python3 manage.py runserver
installed. Proceed to create and activate a Python virtual environment using pyenv.
Then, install the two necessary Python libraries: Django and Django Rest Framework.
,
$pip3 install django
$pip3 install djangorestframework
>
Create a new Django project and navigate into the folder.
,
$ django-admin startproject my_django_project
$ cd my_api_project
>
Create and register a new app by adding a path to the config file of the app.
,
$ django-admin startapp my_rest_api
>
Open my_django_project/settings.py and add the following to the INSTALLED APPS array.
INSTALLED_APPS = [
…
‘rest_api_framework’,
‘my_rest_api.apps.MyRestApiConfig’
] To ensure you have everything set up, run:
,
$ python3 manage.py runserver
>
Going to http://
,
$ django-admin startproject my_django_project
$ cd my_api_project
,
$ django-admin startapp my_rest_api
,
from rest_framework import viewsets
from my_rest_api.serializers import PlayerSerializer, ClubsSerializer
from my_rest_api.models import Player, Clubs
class PlayerViewSet(viewsets.ModelViewSet):
queryset = Player.objects.all()
serializer_class = PlayerSerializer
class ClubsViewSet(viewsets.ModelViewSet):
queryset = Clubs.objects.all()
serializer_class = ClubsSerializer
.
,
$pip3 install django
$pip3 install djangorestframework
.
,
$pip3 install django
$pip3 install djangorestframework
.
,
$ django-admin startproject my_django_project
$ cd my_api_project
:
,
After defining the viewsets, use the DRF router to route an API ray md point to the respective view set. Create a new file my_rest_api/urls.py
from django.urls import include, path
from rest_framework import routers
from my_rest_api.views import PlayerViewSet, ClubsViewSet
router = routers.DefaultRouter()
router.register(r’player’, PlayerViewSet)
router.register(r’clubs’, ClubsViewSet)
urlpatterns = [
path(”, include(router.urls)),
]
,
$pip3 install django
$pip3 install djangorestframework
,
$pip3 install django
$pip3 install djangorestframework
,
$pip3 install django
$pip3 install djangorestframework
/ in your browser will reveal a success message.
,
$ django-admin startapp my_rest_api
style=”text-align: center;” align=”center”>Create Your Django Models,
$ django-admin startapp my_rest_api
>
Going forward, you will be editing the files in the my_rest_api folder. We will start by adding Player and Clubs into my_awesome_api/models.pay
,
from django.db import models
class Clubs(models.Model):
name = models.CharField(max_length=100)
classification = models.CharField(max_length=100)
country = models.CharField(max_length=100)
class Player(models.Model):
name = models.CharField(max_length=100)
birth_year = models.CharField(max_length=10)
eye_color = models.CharField(max_length=10)
clubs = models.ForeignKey(Clubs, on_delete=models.DO_NOTHING)
>
After adding your models, run the migration to instruct Django to add
,
$ django-admin startapp my_rest_api
new tables to the SQLite database.
,
$ python3 manage.py makemigrations
$ python3 manage.py migrate
>
,
$ django-admin startapp my_rest_api
style=”text-align: center;” align=”center”>Set up DRF Serializers,
$ django-admin startapp my_rest_api
>
JSON is the standard for transferring data via REST API. Therefore, you need to set up a way to serialize your models before sending them out as a response. DRF has an inbuilt support to convert the Player and Clubs model into JSON. Create a new file my_rest_api/serializers.py
,
from rest_framework import serializers
from my_rest_api.models import Player, Clubs
class PlayerSerializer(serializers.ModelSerializer):
class Meta:
model = Player
fields = (‘name’, ‘birth_year’, ‘eye_color’, ‘clubs’’)
class ClubsSerializer(serializers.ModelSerializer):
class Meta:
model = Clubs
fields = (‘name’, ‘classification’, ‘country’)
>
,
$ django-admin startapp my_rest_api
style=”text-align: center;” align=”center”>Set up URI endpoints to view your data,
$ django-admin startapp my_rest_api
>
Now, we need to create a view to the API and connect it to the Django URLs. We will start by adding
,
$ django-admin startapp my_rest_api
view sets for each of our models. Create a new file my_rest_api/views.py
,
from rest_framework import viewsets
from my_rest_api.serializers import PlayerSerializer, ClubsSerializer
from my_rest_api.models import Player, Clubs
class PlayerViewSet(viewsets.ModelViewSet):
queryset = Player.objects.all()
serializer_class = PlayerSerializer
class ClubsViewSet(viewsets.ModelViewSet):
queryset = Clubs.objects.all()
serializer_class = ClubsSerializer
>
,
After defining the viewsets, use the DRF router to route an API ray md point to the respective view set. Create a new file my_rest_api/urls.py
from django.urls import include, path
from rest_framework import routers
from my_rest_api.views import PlayerViewSet, ClubsViewSet
router = routers.DefaultRouter()
router.register(r’player’, PlayerViewSet)
router.register(r’clubs’, ClubsViewSet)
urlpatterns = [
path(”, include(router.urls)),
]
>
Then, we will connect the main Django URL to my_django_project/urls.py to the app’s URL file.
,
from django.urls import path, include
urlpatterns = [
path(‘football/’, include(‘my_rest_api.urls’)),
]
>
The routers will automatically generate the URLS to support the desired API methods (GET, POST, and PUT).
Visit the API at
,
$ django-admin startapp my_rest_api
,
from rest_framework import viewsets
from my_rest_api.serializers import PlayerSerializer, ClubsSerializer
from my_rest_api.models import Player, Clubs
class PlayerViewSet(viewsets.ModelViewSet):
queryset = Player.objects.all()
serializer_class = PlayerSerializer
class ClubsViewSet(viewsets.ModelViewSet):
queryset = Clubs.objects.all()
serializer_class = ClubsSerializer
.
,
$pip3 install django
$pip3 install djangorestframework
.
,
$pip3 install django
$pip3 install djangorestframework
.
,
$ django-admin startproject my_django_project
$ cd my_api_project
:
,
After defining the viewsets, use the DRF router to route an API ray md point to the respective view set. Create a new file my_rest_api/urls.py
from django.urls import include, path
from rest_framework import routers
from my_rest_api.views import PlayerViewSet, ClubsViewSet
router = routers.DefaultRouter()
router.register(r’player’, PlayerViewSet)
router.register(r’clubs’, ClubsViewSet)
urlpatterns = [
path(”, include(router.urls)),
]
,
$pip3 install django
$pip3 install djangorestframework
,
$pip3 install django
$pip3 install djangorestframework
,
$pip3 install django
$pip3 install djangorestframework
/football/clubs/ or
,
$ django-admin startproject my_django_project
$ cd my_api_project
,
$ django-admin startapp my_rest_api
,
from rest_framework import viewsets
from my_rest_api.serializers import PlayerSerializer, ClubsSerializer
from my_rest_api.models import Player, Clubs
class PlayerViewSet(viewsets.ModelViewSet):
queryset = Player.objects.all()
serializer_class = PlayerSerializer
class ClubsViewSet(viewsets.ModelViewSet):
queryset = Clubs.objects.all()
serializer_class = ClubsSerializer
.
,
$pip3 install django
$pip3 install djangorestframework
.
,
$pip3 install django
$pip3 install djangorestframework
.
,
$ django-admin startproject my_django_project
$ cd my_api_project
:
,
After defining the viewsets, use the DRF router to route an API ray md point to the respective view set. Create a new file my_rest_api/urls.py
from django.urls import include, path
from rest_framework import routers
from my_rest_api.views import PlayerViewSet, ClubsViewSet
router = routers.DefaultRouter()
router.register(r’player’, PlayerViewSet)
router.register(r’clubs’, ClubsViewSet)
urlpatterns = [
path(”, include(router.urls)),
]
,
$pip3 install django
$pip3 install djangorestframework
,
$pip3 install django
$pip3 install djangorestframework
,
$pip3 install django
$pip3 install djangorestframework
/football/player/
You can perform different requests to both the player and clubs and end points using the browseable api.
,
$ django-admin startapp my_rest_api
style=”text-align: center;”>Conclusion,
$ django-admin startapp my_rest_api
>
The simplicity of configuring DRF to start building REST APIs cannot be overemphasized. This extends the Django principle of creating elegant solutions with very little code. To learn more on how to use this new skill, visit the DRF’s documentation.