REST is one of the methods we usually use in creating API’s. One of the advantages of REST in Django is that we can make it quickly and easily. So, we can deploy our Django apps as soon as possible.
There is a library for Django named “Django REST Framework (DRF)” that we can use for developing REST API.
DRF requires the following:
- Python (3.5, 3.6, 3.7)
- Django (1.11, 2.0, 2.1, 2.2)
DRF recommend and only officially support the latest patch release of each Python and Django series.
Now, we will create a simple application to implement REST in Django.
Initial Preparation.
- Make sure virtualenv and pip has been configured well.
- Install Django with pip:
pip install Django
. - Create a new project with
django-admin startproject drf_project .
. Note:drf_project
is my project name. You can use another project name.
Install DRF.
- We can install DRF with
pip install djangorestframework
. - Add
rest_framework
toINSTALLED_APPS
indrf_project/settings.py
file.
- We can use browsable API to our application. Modify the root
urls.py
as below.
Run Our Project.
- We need to migrate default migration to database with
python manage.py migrate
. Note: I use default sqlite3 as database. You can set custom database as you want. - Create a superuser with
python manage.py createsuperuser
. Then fill the credential as you want. - Run our django project with
python manage.py runserver
. - Open your browser then type
localhost:8000/api/
.
Create an Example Application.
We will create an application called books
application. The books
application have two models, Author
and Book
.
- Create a new application with
django-admin startapp books drf_project/books/
. Note: I usually put the application inside of project folder.
- Add
drf_project.books
toINSTALLED_APPS
indrf_project/settings.py
file.
- Modify
drf_project/books/models.py
as below.
- Run makemigrations with
python manage.py makemigrations books
. - Run migrate with
python manage.py migrate
. - Create a serializer file named
drf_project/books/serializers.py
. Serializer used for representing data in JSON. Serializer usually uses based on a model but it also can use without a model. Check https://www.django-rest-framework.org/api-guide/serializers/ for another reference about serializer. - Modify
drf_project/books/serializers.py
as below.
- Create a viewset. Viewset is like
views
in Django. It used to handle request and response. Check https://www.django-rest-framework.org/api-guide/viewsets/ for another reference about viewset. Modifydrf_project/books/views.py
as below.
- Modify
drf_project/urls.py
as below.
Test Our API.
We need to test our API to ensure that API will work as expected. We can test our API with Django browsable API.
- Run our django project with
python manage.py runserver
. - Open your browser then type
localhost:8000/api/
.
As you can see in the pictures above. You can browse API from browser and insert data via an available form. You can test API with other tools like Postman or via cURL.
- Add pagination to control how many objects per page are returned. To enable, add the following lines to
drf_project/settings.py
file.
That’s it. If you have a question, please write in comments section. Thanks for reading. See you.
Reference: https://www.django-rest-framework.org/
Read: Install Odoo 12 and Wkhtmltopdf on Ubuntu 18.04 or Debian 9