WEBVTT
Kind: captions
Language: en

00:00:00.089 --> 00:00:08.639
Next we're going to add the put patch
and delete methods to our API view load

00:00:08.639 --> 00:00:13.980
up the atom editor and open up the views
dot py file where we have defined our

00:00:13.980 --> 00:00:20.310
hello API view we're going to add three
additional functions to our API view to

00:00:20.310 --> 00:00:27.760
handle HTTP put HTTP patch and HTTP delete
request methods

00:00:27.760 --> 00:00:29.160
Let's start by adding a

00:00:29.160 --> 00:00:35.010
new function for the HTTP put method so
just like we did for get and post we

00:00:35.010 --> 00:00:43.350
define it by typing def put and then we
give it the arguments self request and

00:00:43.350 --> 00:00:50.280
in this case we're going to add a pk
equals none and then below that we're

00:00:50.280 --> 00:00:58.980
going to add a doc string and we're going to write handle updating an object so this

00:00:58.980 --> 00:01:06.450
defines a new method for handling HTTP
put requests HTTP put is often used to

00:01:06.450 --> 00:01:09.060
update an object

00:01:09.060 --> 00:01:13.799
what you do is you make
a request with HTTP put and it will

00:01:13.799 --> 00:01:19.229
update the entire object with what
you've provided in the request so this

00:01:19.229 --> 00:01:24.409
is different from a HTTP patch which I'm
going to explain in the next function

00:01:24.409 --> 00:01:31.380
when you're doing HTTP put you typically
do it to a specific URL primary key

00:01:31.380 --> 00:01:35.939
that's why we have this PK but we
default it to none in case we don't want

00:01:35.939 --> 00:01:40.590
to support the PK in this particular API
view but usually what you would do is

00:01:40.590 --> 00:01:45.960
you would do a put to the URL with the
ID of the object that you're updating

00:01:45.960 --> 00:01:50.360
and that's what we use this PK for this
is to take the ID of the object to be

00:01:50.369 --> 00:01:54.689
updated with the put request we're not
actually going to be updating an object

00:01:54.689 --> 00:01:59.310
we're just going to return a standard
response just to demonstrate how you can

00:01:59.310 --> 00:02:05.750
add the put method to our API view
and test it in the browser so type return

00:02:05.750 --> 00:02:12.030
response and we'll just return a
dictionary with method and then the

00:02:12.030 --> 00:02:16.170
method that we made which
is put so this is where you would put

00:02:16.170 --> 00:02:22.460
any logic that you wanted to do whenever
you did a HTTP put request to the API

00:02:22.460 --> 00:02:33.210
next we're going to add a HTTP patch so
type def patch self request and PK equals

00:02:33.210 --> 00:02:42.870
none and this is handle a partial update
of an object so what you would typically

00:02:42.870 --> 00:02:48.620
use HTTP patch for is to do an update but
only update the fields that were

00:02:48.620 --> 00:02:55.950
provided in the request so if you had a
first name and a last name field and you

00:02:55.950 --> 00:03:01.560
made a patch request with just providing
the last name it would only update the

00:03:01.560 --> 00:03:06.510
last name whereas if you did a put
request and you only provided the last

00:03:06.510 --> 00:03:11.760
name then in that case it would remove
the first name completely because HTTP

00:03:11.760 --> 00:03:15.990
put is essentially replacing an object
with the object that was provided

00:03:15.990 --> 00:03:23.220
whereas patch is only updated the fields
that were provided in the request okay

00:03:23.220 --> 00:03:27.870
so just like we did with the HTTP put
method let's just return a response here

00:03:27.870 --> 00:03:35.160
so return response and we'll write
method and we'll write patch in the

00:03:35.160 --> 00:03:36.860
method here

00:03:36.860 --> 00:03:40.709
Okay so these are the two
update requests or typically used for

00:03:40.709 --> 00:03:46.940
updating objects next we're going to
create a delete request so def delete

00:03:46.940 --> 00:03:56.190
self request PK equals none and then
we'll write a doc string delete an

00:03:56.190 --> 00:03:58.400
object

00:03:58.400 --> 00:04:02.400
As you can imagine the delete
request is used for deleting objects in

00:04:02.400 --> 00:04:16.049
the database so for the delete one let's
return response method delete okay so

00:04:16.049 --> 00:04:21.600
make sure you save that file and that's
how you add the HTTP put patch and delete

00:04:21.600 --> 00:04:25.820
methods to an API view

