Creating Python APIs - The Django REST framework. Building a Reddit clone - Serializers.[3/n]
Welcome back to my API series, where I create and integrate an API to our app. This is part 3 of the series, I highly recommend to read the 1st two posts to be able to follow through with this post.
Part 1 : Creating Python APIs - The Django REST framework : Basics[1/n]
Part 2 : Creating Python APIs - The Django REST framework. Building a Reddit clone - Models.[2/n]
So in the last part, we created a model. Now it's time to start building the API. There are two ways of going about that:
A) You can create the API from scratch.
OR
B) You can use the Django REST framework which does all the heavy lifting for us.
You could not go wrong with either of the two but using the first option, although as enticing as it sounds can get very complicated very fast.
I want to make understanding APIs easier so I'll be using the second method and I promise it'll leave you hooked. Once you get an idea how API works and what you can do with it you can get your hands dirty with the 1st option.
You can read about the Django REST framework here: It is very well documented and very beginner friendly.
There are wonderful tutorials to get you acquainted with the framework. Do give it a read.
So now we'll install the framework using the pip command in our terminal/command prompt:
pip install djangorestframework
Remember the word djangorestframework is all one word with no spaces
After we've successfully installed Django REST framework, now it's time to add it into our project.
We need to add rest_framework
under the Installed apps in settings.py as below:
Now you might be curious about why is it called Django REST framework.
So REST API is an API that follows a certain standard. It has it's own architectural style which you can read about here.
Now that we're done with this, lets talk about serializers.
What are serializers?
Serializers are a way to be the middleman between the models and the APIs. So it basically converts the Django models into json objects and vice versa.
We'll create a new file in our posts app called the serializers.py (You can name this anything you want but this name is what the REST framework suggests)
The explanation of what each line does is inline in the comments:
After this is done, save this and now we will create API urls. So we will begin by adding new path in the urls.py file as below:
Now, you might be confused as to what the views.PostList.as_view()
mean as we don't have that view yet. Don't worry, we will create that view next.
You can read more about Generic views that we imported in views here
This is all we need for our very first API call.
Now run the server and if you go to http://127.0.0.1:8000/
you'd see the following screen.
But don't worry that's completely normal, since we created a url for api/posts, so if you edit the url to display http://127.0.0.1:8000/api/posts
You should see the following screen.
Congratulations! You just made your first API call, although this might not look like much but this is a massive step. The reason it doesn't show any data because we don't have anything saved in the database yet.
In the next posts we'll diver deeper into adding and displaying more information.
Hope you enjoyed this post! If you happen to like it, feel free to share. You can also follow me on Twitter on my coding journey.