Creating Python APIs - The Django REST framework. CRUD operations using API [12/n]

Saurav Jalui's photo
Saurav Jalui

Published on Nov 26, 2020

3 min read

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:

image.png

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.

image.png

The following is what you would see when you run the server and visit the link 127.0.0.1:8000/api/todos

Screenshot_2020-11-26 Todo List Create – Django REST framework(2).png

You can add a todo using the tabs below in the window.

todo.png

You'd be able to see all the todos if you use the link 127.0.0.1:8000/api/todos

create.png

If you would go to the main app, you would be able to see the todo that you added using the api.

image.png

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.

 
Share this