Let's go ahead and add our serializer to
our API view. So we've defined our serializer
here in the serializers.py file.
Now it's time to add it to the views.py
file in our hello API view.
We're going to start by adding a
couple imports here at the top. The
first one we're going to import is the
serializers file which we created in the
previous lesson. So we're going to type
from . import serializers. So as I
mentioned previously the "." imports
from the root directory. So we're
importing our serializers.py module
here. The next import is the status.
The status is a list of, or contains a list
of different HTTP response status codes.
So you may have heard of HTTP 404 or
HTTP 505 now these status codes are all
described in the status object of the
rest framework. So let's type from rest
framework import status. Okay I'm going
to save the file there and now I'm going
to come down to the hello API view
class. Let's tell Django rest framework
what serializer we want to use to
describe the data that we handle with
this API view. So right below the dock
string above the get
function let's add a serializer variable
and let's reference our serializer that
we created in the previous lesson. So
type serializer_class =
serializers.HelloSerializers. So this tells
Django or the tells the Django rest
framework that the serialize class for
this API view is going to be the
serializers
which is here, hello serializer class.
Okay so once we describe
the class let's create a post function.
This is going to be the logic that gets
called when you make a
HTTP POST request to our API view. So type
def to define the function, post and
we're going to use the parameters self
and request. Alright and let's give the
doc string "Create a hello message with
our name." So when you make a post to our
API view we're going to return a message
that includes the name that was posted
to the API.
Okay so first we're going to set our
serializer so we're going to create a
variable that is our serializer object. So
type serializer = serializers.HelloSerializer
and we're going to pass
in the data that is handed to this post
request.
So whenever one of these HTTP method
functions is called there's a request
object here. The request object contains
details of the request that was made to
the API call. That includes the data that
you posted, so when you do a HTTP
post you always have a payload of data.
That is included in the request
object and I'm going to pass it
through to our serializer. So it knows
which data to base the serializer object
on. So after the hello serializer in the
brackets let's type data = request.data
okay so this creates a serializer object
with our hello serializer and it passes
in the request data to the data
attribute. The next thing to do is
validate that the data that is passed in is
valid. So we're going to do an if
statement here that checks that the
serializer has valid data passed into.
So type if serializer.is_valid
so if it's valid we want to return a
response. So first we're going to set the
name a variable called name =
serializer.data.get name
So once we call
is valid all of the data that's passed
into the serializer is available in this
data dictionary. We can retrieve
specific fields by typing the serializer.data
using the . get function and
then the name of the field we want to
get the data from. So in this case we
want to get the name that the user
entered and assign it to a variable
called name. Once we've done that we can
create a message based on that name, so
we're just going to create a simple
string we're going to write message =
and create a string hello and
we're going to use the python string,
the proper python string notation here.
This curly brackets 0 . format and
then the name. So what this does is it
creates a string and then in the string
we're passing in the name using the
format function of the string. This is a
function in Python this is how you
should create strings properly so if you
wanted to create multiple different or
insert multiple different items in the
string you would use these curly
brackets and use the order that you
want them to go in so 0 1 2 3 and so on
and you would add them to this format. We
only want to insert one items in a
string so we're just going to type 0 and
we're just going to pass
the name in to the format. Okay so now
that we've assigned a name and a message
let's return it as a response object.
So type return response
and then we're going to pass the dictionary
that has a key message and we're going
to pass in the message object to that
message key. Okay so that's all good if
the data is valid but if the user
entered invalid data when it was passed
to the serializers and the validation
failed we want to return the error
message to the user so they know what
they did wrong. So we're going to put an
else statement here and we're going to
type return and we're going to return a
response object but this time we're
going to put the data as the serializers
or the serializer.errors so if you
call is valid and it returns false then
there'll be an errors dictionary
assigned to the serializer. This will
contain a list of all the errors that
occurred when we were validating the
data. The second thing we're going to
specify in the request is the status
code so we want to give an accurate
status code so that the app or whatever
is consuming our API knows what went
wrong. We're going to type status
= status.HTTP_400_bad underscore request
so the
HTTP 400 bad request is the standard
status code to be used when you do a
bad request to an API. So a bad request
is either you called something that you
weren't supposed to call, or you put
data in there that you weren't supposed
to put so there was an invalid data so
it was a bad request to the API. I'm
going to bring this down to the line
below so that we keep it within this
maximum line limit here and that's all
we need to do to configure our
serializer
in our API view. So you can save the file
That's how you create a post request or a post
function and add a serialize class to
an API view. Thank you so much for
watching and I'll see you in the next
lesson.