Table Of Contents
* π€INTRO
* πURLS
* π¨π»βπ»SERIALIZERS
* πβπ¨VIEWS
* πΊMAP URL
* 1οΈβ£GET SPECIFIC RECORD
* π£OUTRO
* πTHANK YOU
π€ INTRO
Hello, dear hackers! Welcome to yet another blog article on "Building the REST API with Python Django". I really appreciate your interest in these kinds of articles, and I am really happy that I can help you learn something new.
If you missed the previous article, check it out:
Article No Longer Available
Everything I do in this article and upcoming articles will be pushed to the repository on my GitHub:
devlazar95 / PythonDjangoTutorial
This is an official repository of the Codespresso Dev.to tutorial, regarding the "Building the REST API using Python Django" series.
PythonDjangoTutorial
This is an official repository of the Codespresso Dev.to tutorial, regarding the "Building the REST API using Python Django" series.
HOW TO RUN PROJECT
- Setup you PostgreSQL database (name: company)
- Edit settings.py and specify DATABASES name to company
- Pull the code from this repository
- Open it up in the PyCharm
- Open terminal and execute pip install -r requirements.txt
- Run python manage.py migrate
- Run python manage.py runserver
Today, we are writing methods for GETting all the data from an API as well as GETting the specific record by record id.
Please feel free to connect with me via Twitter, Instagram or LinkedIn
Let's start! π
π URLS
We already have our urls.py inside our company folder. But what we want to achieve is to make our API URLs map as follow:
- http://127.0.0.1:8000/api/ - BASE URL
- http://127.0.0.1:8000/api/employee/ - Return data for all employees in the database
- http://127.0.0.1:8000/api/employee/:id - Manipulate specific employee
- http://127.0.0.1:8000/api/sector/ - Return data for all sectors in the database
- http://127.0.0.1:8000/api/sector/:id - Manipulate specific sector
- http://127.0.0.1:8000/api/project/ - Return data for all project in the database
- http://127.0.0.1:8000/api/project/:id - Manipulate specific sector
To make all routes map into /api/... we will create another file inside our API folder, name it urls.py
Before we proceed with this file, we need to create our serializer and views
π¨π»βπ» SERIALIZERS
Serializers allow complex data such as querysets and model instances to be converted to native Python datatypes that can then be easily rendered into JSON, XML, or other content types. Serializers also provide deserialization, allowing parsed data to be converted back into complex types, after first validating the incoming data.
The serializers in the REST framework work very similarly to Django's Form and ModelForm classes. We provide a Serializer class which gives you a powerful, generic way to control the output of your responses, as well as a ModelSerializer class which provides a useful shortcut for creating serializers that deal with model instances and querysets.
In our serializers.py add the following code (note that I omitted the code regarding UserSerializer and GroupSerializer):
from company.API.models import Employee
class EmployeeSerializer(serializers.ModelSerializer):
class Meta:
model = Employee
fields = '__all__'
This means that we will serialize our Model Employee and include all the fields that our model offers.
πβπ¨ VIEWS
Each view we create will handle specific logic and will map the response with the specific route. Let's create our first view, which will get all the data regarding employees.
In your views.py file add the following code:
from company.API.serializers import UserSerializer, GroupSerializer, EmployeeSerializer
from company.API.models import Employee
from rest_framework.response import Response
from rest_framework.views import APIView
class EmployeeList(APIView):
"""
List all employees
"""
def get(self, request, format=None):
employees = Employee.objects.all()
serializer = EmployeeSerializer(employees, many=True)
return Response(serializer.data)
πΊ MAP URL
Before we add some code to the newly created urls.py, go to the urls.py file inside the company folder, and in the urlpatterns block add the following code:
path('api/', include('company.API.urls'))
This part of the code will consider the http://127.0.0.1:8000/api/ - the base URL - but that path will map all the paths inside our newly created urls.py.
Now, we have our Employee serializer and Employee View, we need a specific endpoint that will map our response data.
In the newly created urls.py, add the following code:
from . import views
from django.urls import path
from rest_framework.urlpatterns import format_suffix_patterns
urlpatterns = [
path('employee/', views.EmployeeList.as_view(), name="employees")
]
urlpatterns = format_suffix_patterns(urlpatterns)
This will map our EmployeeList view response to http://127.0.0.1:8000/api/employee/
Now, we should be ready to get the data from the server (please use your admin panel to add some data for employees before proceeding).
Run the following command:
python manage.py runserver
Click here to see the result in your browser or manually type in your URL bar http://127.0.0.1:8000/api/employee/
Since I have only one record in the database, here is my response:
Congratulations! π You created your first GET request, you could also send the request using the Postman, for now, I will stick to the browser.
1οΈβ£ GET SPECIFIC RECORD
Similarly, when we want to get the specific record by the unique identifier, we define our view that we will name EmployeeDetails, and after that, we map our view to the specific URL.
Here is the view for getting specific Employee record by the unique identifier
class EmployeeDetails(APIView):
"""Retrieve an employee instance"""
def get_object(self, pk):
try:
return Employee.objects.get(pk=pk)
except:
raise Http404
def get(self, request, pk, format=None):
employee = self.get_object(pk)
serializer = EmployeeSerializer(employee)
return Response(serializer.data)
As you can see, we are using get_object function where we pass the primary key of an instance we are requesting. We have a try-except block of code, where we determine if such an instance exists, or if it does not, we raise an error. An Http404 error is imported as from django.http import Http404
;
Now, we should map our URL, add the following code to the urlpatterns inside the newly created urls.py file.
path('employee/<slug:pk>', views.EmployeeDetails.as_view(), name="employee")
Ok, we should be ready to retrieve the specific data for a single Employee instance. First, you need a primary key, if you execute http://127.0.0.1:8000/api/employee/
you will see that each employee has an employee_id attribute, copy one of the employee_ids and execute this route https://127.0.0.1:8000/api/employee/paste_your_employee_id_here
you should GET a single instance with the specific employee_id you requested.
Here is mine π½
π£ OUTRO
So, we created views for getting all employees' data and for getting a single employee data. The logic is the same for the Project and Sector model. I encourage you to try, and write those views, serializers and urls by yourself, but If you get stuck, you can always look for the solution on my GitHub. In the next article, we will create the POST and PUT methods.
π THANK YOU FOR READING!
Please leave a comment, tell me about you, about your work, comment your thoughts, connect with me!
β SUPPORT ME AND KEEP ME FOCUSED!
Have a nice time hacking! π
Top comments (5)
How do you deal with abstraction when the actions you do on your web page affect many entities?
If your API entities are the same as your DB entities, don't you run the risk of having transactional problems and N+1 problems?
Thank you for commenting. There are certainly a couple of ways to do it, and probably some that I am not aware of (In the software development, we are all in the learning process - foreeveer π) - On top of my mind is select_related or prefetch_related, but, essentially the goal is for beginners to get a grasp of building an API with Django. This is a simple, "school" example, where I want to get beginners interested in the matter and to get them just start. And of course, in some future articles, we may discuss N+1, 2N+1....problems. Thank you, again. π
I think you are referring to the server side.
I'm referring to the client side.
If you model your API in a normalized way. How do you generally approach a situation where the UI needs to affect several entities at once? Do you have denormalized abstractions in your API?
Well, let's say we have multiple entities, each of which can be standalone entities (no foreign keys) but are referenced in some pivot tables for example. And if I had a situation where the FE sends me a request with all necessary data to create and populate multiple entities, if validation passes, we could first create entities that are not dependable, and after that, we can create all other dependencies. If for some reason it breaks, we can always roll back what we previously created. It is a bit more complex than this, but I would really like to know how do you handle N+1 problems?
What I do is I have denormalized abstractions at the REST/Json level.
I model the API based on how the webpages/users want to use them rather than on DB normalization concepts. So that I minimize roundtrips.
Then the backend takes care of parsing the JSON and pumping it all into the correct tables.
This way the client does everything in a single transaction by sending all the form data in one single JSON (no matter how many entities may be affected).
Some comments may only be visible to logged-in visitors. Sign in to view all comments.