Next we're going to add the retrieve update partial update and delete
functions to our hello view set
So these functions are going to
represent the different HTTP methods that we've added to our API view but for
our view set let's start by adding the serializer class and we can use the same
serializer that we created for our API view with the name field we can
share the same serializer for both of our view sets and we specify the
serializer in our view set the same way as we do for our API view and that is by
typing serializer underscore class equals serializers dot
hello serializer then let's add the create function to our view set so
create a new function underneath the list function and call it def create
and pass in the self and request parameters
then we're going to add the
doc string create a new hello message and that's what this create function is
going to do in our view set and what we'll do is we will retrieve our
serializer by doing serializer equals self dot serializer class underscore class
data equals request dot data so we pass in the data that was made in the request
and we passed that in as the data attribute of our serializer
which we retrieve using the serializer class
Ok now that we have our sterilizer
we can validate it and we validate it by typing if serializer dot is underscore
valid then we will set name equals serializer dot validated data dot get
and we'll retrieve the name field and then we'll create a message message
equals and we'll use the F string here to do hello
and then we'll insert the name variable into our string and we'll add an
exclamation mark at the end okay now we can return our response object so we'll
do return response and we'll write message is equal to message okay so
that's if the serializer data is valid if the data is invalid then we'll handle
that with the else statement and we'll return response and we'll return
serializer dot errors so the list of errors that happened in the validation
and then we'll add a status code equals status dot HTTP 400 bad request okay that's
the create function for our view set next let's create the retrieve function
so the retrieve function is for retrieving a specific object in our view
set so you would typically pass in a primary key ID to the URL in the request
and that will return or retrieve the object with that primary key ID so we're
just going to create a new function below the create function here def
retrieve and we'll accept the arguments or the parameters self request and PK
equals none so PK is for the primary key in the doc string we'll add handle
getting an object by its ID and then we'll just return a response or return
response and we'll write HTTP method get
so that's our retrieve function the next
one we'll add is the update which maps to a HTTP put on the primary key item of
our view set so you'll see how this will work in the browser when we demonstrate
in the next video but for now let's create a new update function so def
update self request PK equals none and we'll write the doc string handle
updating an object and we'll return response HTTP method put okay the next
one we'll add is the partial update so def partial update self requests PK
equals none and we'll write the doc string handle updating part of an
object and we'll return response HTTP method patch so the partial update maps
to the patch request and that is for updating the fields that were provided
in the request only so part of the object
finally we will add the destroy
function def destroy self request PK equals none and then the doc
string handle removing and objects and as you can imagine destroy maps to
delete so if you wish to remove an object then you would call HTTP delete
on our view set which would then call this destroy function and run the code
that we write in here so let's just return response HTTP method delete
okay so save the file and that's how you add the retrieve update partial update
and destroy functions to a view set