Next we're going to add the put patch and delete methods to our API view load
up the atom editor and open up the views dot py file where we have defined our
hello API view we're going to add three additional functions to our API view to
handle HTTP put HTTP patch and HTTP delete request methods
Let's start by adding a
new function for the HTTP put method so just like we did for get and post we
define it by typing def put and then we give it the arguments self request and
in this case we're going to add a pk equals none and then below that we're
going to add a doc string and we're going to write handle updating an object so this
defines a new method for handling HTTP put requests HTTP put is often used to
update an object
what you do is you make a request with HTTP put and it will
update the entire object with what you've provided in the request so this
is different from a HTTP patch which I'm going to explain in the next function
when you're doing HTTP put you typically do it to a specific URL primary key
that's why we have this PK but we default it to none in case we don't want
to support the PK in this particular API view but usually what you would do is
you would do a put to the URL with the ID of the object that you're updating
and that's what we use this PK for this is to take the ID of the object to be
updated with the put request we're not actually going to be updating an object
we're just going to return a standard response just to demonstrate how you can
add the put method to our API view and test it in the browser so type return
response and we'll just return a dictionary with method and then the
method that we made which is put so this is where you would put
any logic that you wanted to do whenever you did a HTTP put request to the API
next we're going to add a HTTP patch so type def patch self request and PK equals
none and this is handle a partial update of an object so what you would typically
use HTTP patch for is to do an update but only update the fields that were
provided in the request so if you had a first name and a last name field and you
made a patch request with just providing the last name it would only update the
last name whereas if you did a put request and you only provided the last
name then in that case it would remove the first name completely because HTTP
put is essentially replacing an object with the object that was provided
whereas patch is only updated the fields that were provided in the request okay
so just like we did with the HTTP put method let's just return a response here
so return response and we'll write method and we'll write patch in the
method here
Okay so these are the two update requests or typically used for
updating objects next we're going to create a delete request so def delete
self request PK equals none and then we'll write a doc string delete an
object
As you can imagine the delete request is used for deleting objects in
the database so for the delete one let's return response method delete okay so
make sure you save that file and that's how you add the HTTP put patch and delete
methods to an API view