Let's begin creating our profiles API by
starting with the serializer class for
our user profile object.
Okay so load up atom and make sure you
have the profiles REST API project open.
So I'm going to add project folder here.
I'm going to go to the workspace and the
profiles REST API and open that. And
locate the serializers file under the
profiles API directory. This is
serializers.py. We're going to create a
serializer in here for our user profile
object. We're going to create what's
called a model serializer. Model
serializes are just like the basic
serializer that we created for our hello
serializer. However they're designed to
be used with models so they have a lot
of built-in functionality that can be
used with the django model classes like
creating objects deleting objects and
things like that.
Let's start by importing our models
module into these serializers class. So
type: from.import models. Okay and
then below our hello serializer class
we're going to create a new class called
the user profile serializer. We're going
to inherit from serializers.ModelSerializer
to inherit from the Django
rest framework model serialize class.
Okay so type: class UserProfileSerializer
and inherit from serializers.ModelSerializer
okay and we're going to write the doc
string "A serializer
for our user profile object."
Okay so we could just define the
different fields that we need for the
serializer but we're going to make use
of the model serializer features and
we're just going to define what is
called a meta class that tells Django
rest framework what fields we want to
take from our model. So underneath the
doc string type: class and then Meta:
and then the first thing we assign in
the meta data of this class is we assign
the model that it's going to point to. So
type: model and we want to point to
models.UserProfile so this tells the
Django rest framework that this model
serializer is going to be used with our
user profile model. The next thing we're
going to do is tell it what fields in
our model we want to use in this
serializer. So we're going to define
something called
fields and we're going to create what's
called a tuple so we're going to open
the bracket and then just basically
create a list of strings, comma separated
strings, inside the brackets. The first
field we want to include is the id field
so that the user id of the user profile.
The next field is the email address so
that's just email and then it's name and
finally we want to include the password
field because we want to be able to
manage the passwords through the
serializer. Okay so we're almost there.
The next thing we're going to do is
define some extra keyword arguments for
our model. And what this does is it tells
Django rest framework or it allows us to
tell Django rest framework special
attributes that we want to apply to
these fields. So in this example we want
the password field to be write only. We
don't want people to be able to see a
password in the system because firstly
they'll only see the password hash
They won't see the real password.
And secondly it's a security flaw if you let
people see passwords of other users. So
we're going to define the extra kwargs
here so type extra_kwargs =
then create a dictionary so that's the two
curly brackets and the
first key we're going to add is password.
So this corresponds to which specific
field we want to add special keyword
arguments to. We're going to choose
password and then : and then
create another sub dictionary inside
this dictionary and these are the
different attributes we can apply to
password. We want to apply the write only
attribute. So type write_only as the key
and as the value type true so this
basically says use the model the user
profile model take the fields id, email
name and password and make sure the
password field is write only so it will
never be read you won't be able to see
it through the serialize and you'll only
be able to write to it from the
serializer. The next thing we're
going to do in our profile serializer
is create a special function that
overrides the create functionality. So we
want to have control over how new users
are created. The reason for this is we
want to be able to assign the password
correctly so we want to call the set
password function on the model when we
assigned a password and not just take
the value the user enters as the password.
Because this way we can encrypt
the password as a hash and it won't just
store the clear text password in the database
So after the meta field, create a new
function by typing def create
We're going to pass the parameters self and
validated_data
Give it the doc string "Create and return
a new user." So when we call the create of
our model serializer the data that's
already been validated gets passed
through as the validated
data parameter we can then use this
validated data parameter to create our
new user object. So below the doc string
let's define a new variable called user =
and we want to create a new user
model object, so type: models.UserProfile
Open the brackets and then
we're going to assign the email =
validated_data email and the name =
validated_data name and below that we
want to type user.set password and
then we want to set the password as the
password the user entered so validated
data password. And then write user.save
so this saves the user object to
the database. And finally we return the
user. Okay so that's our
user profile serializer
that we're going to be using to create
our user API endpoint. Thank you for
watching. I'll see you in the next lesson.