Welcome back to my series on creating Python APIs using Django REST framework.
Today we will add the basic functionality of CRUD (Create, Read, Update, Delete) in our API view.
First, let's add the option to create new todo using API.
For that we need to add the new url to the urls.py file of api folder.
Follow along with me as below:
Add the following to the views.py file.
Notice we use the same class as class TodoCompletedList. Instead of using the ListAPIView from generics we use the ListCreateAPIView because we want to create a todo using the api.
We make sure we use the TodoSerializer and permission class is to be authenticated because we want to make sure only authorized users have the ability to create todos.
We also define a create function to make sure it saves the todo under the user that is logged in.
The following is what you would see when you run the server and visit the link 127.0.0.1:8000/api/todos
You can add a todo using the tabs below in the window.
You'd be able to see all the todos if you use the link 127.0.0.1:8000/api/todos
If you would go to the main app, you would be able to see the todo that you added using the api.
In the next article, we will see how we can use the api to complete the todos.
- You can view the entire code on my github profile.
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.