Let's start creating our user profiles API by creating a serializer for our
user profile objects open up atom editor and make sure you have our project open
we're going to add a new serializer to the serializers dot py file of our
profiles API project the serializer that we're going to add here is going to be a
model serializer it's very similar to a regular serializer except it has a bunch
of extra functionality which makes it really easy to work with existing django
database models
So we're going to create a new serializer called user profile
serializer we're going to base it off of these serializers dot model sterilizer class
and we're going to connect it up to our user profile model that we created
previously
Let's start by importing our user profile model into the
top of the imports here so from profiles API import models this
will allow us to access our user profile model that we previously created
underneath the hello serializer let's create a new sterilizer called user
profile serializer by typing class user profile serializer and we're going to base
our class from serializers dot model serializer
give it the doc string serializes a user profile object then what we're going to
do is define a meta class the way that you work with model serializers is you
use a meta class to configure the serializer to point to a specific model
in our project so create a new meta class by typing class meta and then in
the class we define a new variable called model so model equals models dot
user profile
this sets our serializer up to point to
our user profile model the next thing that you do with a model sterilizer is
you need to specify a list of fields in our model that we want to manage through
our serializer so this is a list of all the fields that you want to either make
accessible in our API or you want to use to create new models with our serializer
so you define the list of fields by typing fields and you pass in a tuple
which is the same as a list but it uses these curved brackets instead of
the square brackets and we're going to define the list or the tuple of
fields that we want to make accessible in our model so we're going to use the
ID field the email the name and the password
so this is the list of fields
that we want to work with we want to make an exception to the password field
because we only want to use this when creating new users in the system we
don't want to allow the users to retrieve the password hash because
there's certain security risks associated with that so we want to make
this password field write only the way you do that is you use the extra keyword
args variable here so type extra underscore kwargs equals and this
is going to be a dictionary and the keys of the dictionary are the fields that
you want to add the custom configuration to so we're only going to provide one
for the password field and then create a new dictionary associated with this
password and we are going to add two more key value pairs the first one is
going to be write underscore only and the value is going to be true so this
says when we create our password field from our model set it to write only
equals true that means you can only use it to create new objects or update
objects you can't use it to retrieve objects so when you do a get you won't
see the password field included in that response
the second thing we're going to do is add a custom style to this and this is
just for the browsable api and what it does is it will set the field type to a
password field which means you won't be able to see the input as you're typing
it so you'll just see the dots or the stars that you would expect in a regular
password input field so let's type style : and then this accepts another
dictionary and we're going to put input underscore type and we're going to give
it the input type of password
Okay so that's the field and model configuration
of our user profile steriliser the next thing we're going to do is we're going
to overwrite the create function by default the model serializer allows you
to create simple objects in the database so it uses the default create function
of the object manager to create the object we want to override this
functionality for this particular serializer so that it uses the create
user function instead of the create function the reason we do this is so
that the password gets created as a hash and not the clear text password that it
would do by default if we didn't override the function the way you
override a create function in a model serializer is you simply define a new
function in the class called def create and pass in the argument self and
validated data
and give it the doc string create and return a new user
Make sure that the function is four characters or at one tab away from the
start of the class because sometimes it can be confusing when you have a meta
class here you don't want to put the create function
inside the meta class you want to make sure it's within the serializer class
here otherwise it won't work properly
Okay so what happens here is whenever we
create a new object with our user profile serializer
it will validate the object or validate the fields provided to the serializer
and then it will call this create function passing in the validated data
so what we want to do here is we want to create and return a new user from our user
profiles model manager so we'll type user equals models dot user profile dot
objects dot create underscore user and then we're going to pass in the
appropriate fields from the validated data so we'll pass in email equals
validated underscore data and we want to retrieve the email field from the
validated data and then we will pass in name equals validated underscore data
and we will get the name key then password equals validated data password
and then finally we will return the new user
okay so we can save that
serializers file and what this does is it will override the create function and
call our create user function that we previously defined here in our user
profile manager so we use this function to create new users in the database and
as you can see this function calls the set password to set the password instead
of just passing all the keys in as plain text
Okay so that's how you create a
serializer for our profiles API