Let's finish off our view set by adding
the create retrieve update partial
update and destroy functions. So load up
atom and locate the views.py file
within our profiles API project. Now come
down to the bottom where we added our
hello viewset class. We're going to add
a serializer class to this the same way
that we added a serializer class to the
hello API view. Viewsets and API views can
use the same serializer.
So we can use the same one that we
created for our hello API view. So just
below the doc string of hello view set
type serializer class = serializers.HelloSerializers
Okay so that assigns of
serializer class to our viewset. Next we
need to add the create function.
So the create function takes care of the
HTTP POST function. So as the name
suggests the purpose of the
create function is to create new objects
in the system. Let's define a create
function and we can use tests. So def
create and let's pass it the parameters
self and request. In that we're going to
give it a doc string "Create a new Hello
message." Close off the doc string and the
first thing we'll do is define the
serializer. We'll create a serializer
object and pass it the request data.
So type serializer = serializers.HelloSerializers
and pass it the data
= request.data. Below that we're
going to check that the serializer is
valid. So that is that they passed valid
data to the serializer. So we'll use
serializer . is valid - oh sorry that
should be an if statement so if
serializer . is valid. So if it's valid
we're going to get the name from the
serializer. Name = serializer.data.get
the name attribute. Below that
we're going to define a message with the
name in it. So we'll type message =
hello and we'll insert a custom
field into the string. So this one
. format and we'll pass in the name. So
this is going to assign the name to the
first number here that we have in the
string.
Okay so below that we're going to return
the response object with the message
containing the name. Return response and
then create dictionary in there and
we'll type the key as message : the
message variable. Okay so we've returned
the object if the input is valid now we
need to take care of the response if the
input invalid. We're going to type else
below there we're going to return
response and in the response we're going
to assign the errors so serializer .
errors and we're going to give it a
status code status HTTP 400 bad request
Let's move this to the line below to
keep within the maximum
character limit. Okay so that's the
create function. That will take care of the
HTTP POST. Next let's add the retrieve
function. The retrieve function takes
care of getting a specific object by its
id so you can use its database id to get
a specific object. Let's define the
retrieve function by typing def
retrieve and let's pass it the
parameters self, request and pk = none
so we'll give it a default none and
we'll give it a doc string "Handles
getting an object by its id." Close off
the doc string and for this one we'll
just return a response with the HTTP
method that it should use so we'll just
return return response the dictionary
HTTP method get. Okay so that takes to
have our retrieve function. Next let's add
the update function. The update
corresponds to the HTTP put method. So
update is used as the name suggests to
update an object so let's create the
update function by typing def update
and give it the parameters self, request and
pk. This would also need a primary key
because you need to know which object is
updating in the database. Add the dock
string "Handles updating an object."
Close off the dock string and let's
return the response HTTP method put. Okay
so that's the update one done. Next let's
add the partial update so the partial
update corresponds to the HTTP patch
method. This is for updating part of an
object so that some fields in an object
but not all of them. So let's type def partial
update self request pk = none and
let's add a doc string "Handles updating
part of an object." And let's return the
response with a dictionary HTTP method
patch. Okay so now we have one more to
add and that's the destroy. So the
destroy function destroys an object or
deletes it from the database. So that
corresponds to the HTTP delete method.
Let's add our destroy function.
def destroy self request pk = none
because again it needs a primary key
to know which object it's destroying or
deleting. Add the doc string "Handles
removing an object." Close off the doc
string and let's return response with a
dictionary HTTP method delete. Okay
so let's save the file. That completes
our viewset thank you so much for
watching and I'll see you in the next
lesson.