On this article, we’ll learn to build our own API with Django framework (for all those Python lovers out there, and all the newbies like me). This article is for my future self, but feel free to suggest anything. Thanks to CodAffection for his great tutorial.
This REST API will help us to create, read, update and delete data from and employee database. This is a very simple example, but once you learn how to build an API, you will be non-stop on your projects; every platform that manipulate data has his own API, such as Twitter and Facebook Graph.
Initializing Django project and creating tables
It’s a good practice to encapsulate all our libraries on environment variables. This helps to avoid mixing pears with apples when we use different libraries on our code. There are few ways to do this, but on this tutorial we’ll be using Anaconda. You can download it here.
Once we have Anaconda installed, we create and activate our environment variable for this project as follows:conda create -n django-rest-api python=3.6
conda activate django-rest-api
Then, we install the Django library and start a new project. We’ll call it django_rest_api.
pip install django
django-admin startproject django_rest_api
Now, we’re able to navigate to the project folder and start a new app.
python manage.py startapp employeeapi
Next, we install the REST Framework package. This will do the magic behind the scenes.pip install djangorestframework
Then, we install the PostgreSQL package.pip install psycopg2
(Sometimes, installing PostgreSQL can be a pain in the ass. If you have a hard time installing this package, please let me know in the comments.)
We add the Django REST Framework and employeeapi apps to our settings.py file
Then, we create a new database on pgadmin4. We’ll name it “restfulapiDB”.
We set up the PostgreSQL database in the settings.py file:
Then, we create our ‘Employee’ model with the fields of each employee:
Once we created our model, we do database migrations:
python manage.py makemigrations employeeapi
python manage.py migrate
Creating REST API
A serializer is needed in order to translate JSON files into Python native data types and viceversa. Thus, we create the serializers.py file, inside the employeeapi app.
Then, we need to create as well the viewsets.py file, in order to config functions to list, retrieve, create, update and destroy data.
We have to map URLs into the EmployeeViewset inside our applications. For this, we’ll create routers.py as follows:
To redirect all the requests of the API, the urls.py file of django_rest_api project folder must be properly configured.
So far, we’ve configured and initialized an API connecting an employee database. Thanks to our urls.py configuration, we are able to make requests to the ‘api/’ url; that is, if we want to use GET, POST, PUT and DELETE web methods, we’ll be mapping those requests with router.py file, as shown in the embedded code.
Retrieving records from employee (list-GET method):
localhost:p/api/employeeRetrieving a single record:
localhost:p/api/employee/<id>
Now, we can run our server with
python manage.py runserver
If we go to the following url:
http://localhost:8000/api/employee/
We should see our REST API framework working.
Making requests to our API
Right now, we don’t have any record created on our Employee table. That’s why we see and empty array on our viewset list (this is marked with red color):
Django REST Framework allows us to create data from an HTML form created on the same page.
We can do a POST request, and the data will be stored on our PostgreSQL database.
And that’s it. Call it a day.