WEBVTT
Kind: captions
Language: en

00:00:00.030 --> 00:00:05.250
Next we're going to add the retrieve
update partial update and delete

00:00:05.250 --> 00:00:11.200
functions to our hello
view set

00:00:11.200 --> 00:00:13.019
So these functions are going to

00:00:13.019 --> 00:00:18.029
represent the different HTTP methods
that we've added to our API view but for

00:00:18.029 --> 00:00:24.930
our view set let's start by adding the
serializer class and we can use the same

00:00:24.930 --> 00:00:30.300
serializer that we created for our API
view with the name field we can

00:00:30.300 --> 00:00:34.290
share the same serializer for both of
our view sets and we specify the

00:00:34.290 --> 00:00:40.170
serializer in our view set the same way
as we do for our API view and that is by

00:00:40.170 --> 00:00:46.710
typing serializer
underscore class equals serializers dot

00:00:46.710 --> 00:00:57.390
hello serializer then let's add the
create function to our view set so

00:00:57.390 --> 00:01:03.120
create a new function underneath the
list function and call it def create

00:01:03.120 --> 00:01:11.100
and pass in the self and request
parameters

00:01:11.100 --> 00:01:12.140
then we're going to add the

00:01:12.150 --> 00:01:18.990
doc string create a new hello message
and that's what this create function is

00:01:18.990 --> 00:01:23.970
going to do in our view set and what
we'll do is we will retrieve our

00:01:23.970 --> 00:01:33.380
serializer by doing serializer equals
self dot serializer class underscore class

00:01:33.380 --> 00:01:43.979
data equals request dot data so we pass
in the data that was made in the request

00:01:43.979 --> 00:01:48.060
and we passed that in as the data
attribute of our serializer

00:01:48.060 --> 00:01:54.160
which we retrieve using the serializer
class

00:01:54.160 --> 00:01:56.009
Ok now that we have our sterilizer

00:01:56.009 --> 00:02:02.340
we can validate it and we validate it by
typing if serializer dot is underscore

00:02:02.340 --> 00:02:13.030
valid then we will set name equals
serializer dot validated data dot get

00:02:13.030 --> 00:02:19.660
and we'll retrieve the name field and
then we'll create a message message

00:02:19.660 --> 00:02:23.920
equals and we'll use the F string here
to do hello

00:02:23.920 --> 00:02:30.280
and then we'll insert the name variable
into our string and we'll add an

00:02:30.280 --> 00:02:37.090
exclamation mark at the end okay now we
can return our response object so we'll

00:02:37.090 --> 00:02:46.480
do return response and we'll write
message is equal to message okay so

00:02:46.480 --> 00:02:52.630
that's if the serializer data is valid
if the data is invalid then we'll handle

00:02:52.630 --> 00:03:00.010
that with the else statement and we'll
return response and we'll return

00:03:00.010 --> 00:03:06.459
serializer dot errors so the list of
errors that happened in the validation

00:03:06.459 --> 00:03:20.410
and then we'll add a status code equals
status dot HTTP 400 bad request okay that's

00:03:20.410 --> 00:03:26.010
the create function for our view set
next let's create the retrieve function

00:03:26.010 --> 00:03:33.489
so the retrieve function is for
retrieving a specific object in our view

00:03:33.489 --> 00:03:40.570
set so you would typically pass in a
primary key ID to the URL in the request

00:03:40.570 --> 00:03:47.590
and that will return or retrieve the
object with that primary key ID so we're

00:03:47.590 --> 00:03:51.720
just going to create a new function
below the create function here def

00:03:51.720 --> 00:04:02.110
retrieve and we'll accept the arguments
or the parameters self request and PK

00:04:02.110 --> 00:04:14.560
equals none so PK is for the primary key
in the doc string we'll add handle

00:04:14.560 --> 00:04:25.220
getting an object by its ID and then
we'll just return a response or return

00:04:25.220 --> 00:04:33.340
response and we'll write HTTP method get

00:04:33.340 --> 00:04:35.630
so that's our retrieve function the next

00:04:35.630 --> 00:04:43.400
one we'll add is the update which maps to
a HTTP put on the primary key item of

00:04:43.400 --> 00:04:47.120
our view set so you'll see how this will
work in the browser when we demonstrate

00:04:47.120 --> 00:04:51.740
in the next video but for now let's
create a new update function so def

00:04:51.740 --> 00:05:01.120
update self request PK equals none and
we'll write the doc string handle

00:05:01.120 --> 00:05:14.270
updating an object and we'll return
response HTTP method put okay the next

00:05:14.270 --> 00:05:22.880
one we'll add is the partial update so
def partial update self requests PK

00:05:22.880 --> 00:05:29.480
equals none and we'll write the
doc string handle updating part of an

00:05:29.480 --> 00:05:41.240
object and we'll return response HTTP
method patch so the partial update maps

00:05:41.240 --> 00:05:49.160
to the patch request and that is for
updating the fields that were provided

00:05:49.160 --> 00:05:53.760
in the request only so part of the
object

00:05:53.760 --> 00:05:55.640
finally we will add the destroy

00:05:55.640 --> 00:06:04.040
function def destroy self
request PK equals none and then the doc

00:06:04.040 --> 00:06:11.180
string handle removing and objects and
as you can imagine destroy maps to

00:06:11.180 --> 00:06:15.800
delete so if you wish to remove an
object then you would call HTTP delete

00:06:15.800 --> 00:06:21.890
on our view set which would then call
this destroy function and run the code

00:06:21.890 --> 00:06:32.530
that we write in here so let's just
return response HTTP method delete

00:06:32.530 --> 00:06:38.210
okay so save the file and that's how you
add the retrieve update partial update

00:06:38.210 --> 00:06:43.509
and
destroy functions to a view set

