Introduction

Welcome to the lesson on updating To-Do items! In this lesson, we will focus on updating existing To-Do items using the Django REST framework. By the end of this lesson, you will be able to create an endpoint that allows users to modify their To-Dos, and you will understand how to test this functionality with HTTP requests.

Imagine a user made a typo when creating a new ToDo in our app and wants to fix it. Updating a resource is a critical part of RESTful architecture, where the PUT method allows you to modify an existing record. In this context, we will build upon the previous lessons where we learned to create and retrieve To-Dos. Let’s dive in!

Recap of Prior Setup

Before we proceed, let's quickly recap the fundamental setup we've done so far. This includes defining our model and serializer for To-Do items. Here’s a brief reminder of what our models.py and serializers.py look like:

myproject/myapp/models.py

myproject/myapp/serializers.py

With these essential components set up, we're ready to create the update view.

Creating the Update API View

To enable updating To-Do items, we will use the UpdateAPIView class from the Django REST framework. Here’s how you can create the Update view:

myproject/myapp/views.py

Explanation:

  • We import generics from rest_framework.
  • We define a class TodoUpdate that inherits from generics.UpdateAPIView.
  • We set queryset to Todo.objects.all(), which retrieves all To-Do items from the database.
  • We assign TodoSerializer to serializer_class to determine the structure of our data.

This view will handle PUT and PATCH requests to update an existing To-Do item. As you can see, creating it is straightforward and very similar to other generics we tried. However, again, it is vital to understand the details of how it is used.

How the PUT Request Works

The PUT method in REST architecture is used to update an existing resource in its entirety. When you use the PUT method, you send the complete representation of the resource you want to update. If parts of the resource are not included in the request, they will be overwritten with default or empty values. This is why it’s important to include all required fields in the PUT request.

For example, consider the following PUT request to update a To-Do item:

In this case, both the task and completed fields are included. If any field were omitted, it would be reset to its default value.

The PATCH Request

The PATCH method is similar to PUT but is used for partial updates. When you use the PATCH method, you only need to send the fields you want to update. This means you can update one or more fields of an existing resource without impacting other fields.

For example, consider the following PATCH request to update only the task field of a To-Do item:

In this case, only the task field will be updated, and the completed field will remain unchanged.

Setting Up the URLs

We need a way to retrieve the details of a specific To-Do item to confirm that it has been updated correctly. For this purpose, we'll use TodoDetail view from the previous unit. To make our views accessible through specific endpoints, we'll configure our URLs in urls.py.

myproject/myapp/urls.py

Explanation:

  • We import the necessary views: TodoUpdate, TodoDetail, and TodoListCreate.
  • We add paths for todo_update and todo_detail with placeholders <int:pk> to identify specific To-Do items by their primary key.
  • To distinguish endpoints for update and retrieve, we add /update/ to the URL mapped to the TodoUpdate view.

This ensures our API can handle requests to update and retrieve specific To-Do items.

Practical Scenarios for Using the Update Endpoint

Here are some practical scenarios where an update endpoint is essential:

  • Correcting Mistakes: Users may make typos or other errors when creating a To-Do item and need to correct those errors.
  • Changing Status: Users may need to mark tasks as completed or change their status back to incomplete.
  • Adding Details: Initially, a user might create a To-Do item with minimal information and later decide to add more details.

By providing an update endpoint, our To-Do application becomes more flexible and user-friendly, allowing users to manage their tasks more effectively.

Testing the Update and Retrieve Views

Let's test the functionality of the update and retrieve views using Python's requests library.

send_request.py

Explanation:

  • We define the base URL for our API.
  • We create a new To-Do item to ensure the database isn't empty and print the response.
  • We update the newly created To-Do item using its ID and print the updated response.
  • We partially update the To-Do item using its ID and print the partial update response.
  • Finally, we retrieve the updated To-Do item and print its details to verify the update.
Review, Summary, and Next Steps

In this lesson, we covered the essentials of updating To-Do items in a Django REST API. You learned how to create UpdateAPIView, configure URL for it, and test the endpoints using HTTP requests.

Key Points Recap:

  • Use UpdateAPIView to update existing resources.
  • Use RetrieveAPIView to fetch details of specific resources.
  • Configure URLs to ensure accessibility of these views.
  • Practical scenarios include correcting mistakes, changing status, and adding details.
  • The PUT method is for full updates, while the PATCH method is for partial updates.

With this knowledge, you can create, retrieve, and update To-Do items in your Django REST API. Make sure to practice these concepts with the following exercises to solidify your understanding.

Congratulations on getting through this lesson. Continue practicing, and you'll become proficient in handling CRUD operations with the Django REST framework. Happy coding!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal