WEBVTT
Kind: captions
Language: en

00:00:00.060 --> 00:00:06.450
Let's start creating our user profiles
API by creating a serializer for our

00:00:06.450 --> 00:00:14.570
user profile objects open up atom editor
and make sure you have our project open

00:00:14.570 --> 00:00:20.070
we're going to add a new serializer to
the serializers dot py file of our

00:00:20.070 --> 00:00:26.070
profiles API project the serializer that
we're going to add here is going to be a

00:00:26.070 --> 00:00:32.399
model serializer it's very similar to a
regular serializer except it has a bunch

00:00:32.399 --> 00:00:37.800
of extra functionality which makes it
really easy to work with existing django

00:00:37.800 --> 00:00:40.300
database models

00:00:40.300 --> 00:00:43.710
So we're going to create a
new serializer called user profile

00:00:43.710 --> 00:00:48.899
serializer we're going to base it off of
these serializers dot model sterilizer class

00:00:48.899 --> 00:00:54.360
and we're going to connect it up to our
user profile model that we created

00:00:54.360 --> 00:00:56.760
previously

00:00:56.760 --> 00:01:03.420
Let's start by importing our user profile model into the

00:01:03.420 --> 00:01:10.799
top of the imports here so from profiles API import models this

00:01:10.799 --> 00:01:15.860
will allow us to access our user profile model that we previously created

00:01:15.860 --> 00:01:20.939
underneath the hello serializer let's
create a new sterilizer called user

00:01:20.939 --> 00:01:29.130
profile serializer by typing class user
profile serializer and we're going to base

00:01:29.130 --> 00:01:35.450
our class from serializers dot model
serializer

00:01:35.450 --> 00:01:43.470
give it the doc string serializes a user
profile object then what we're going to

00:01:43.470 --> 00:01:49.079
do is define a meta class the way that
you work with model serializers is you

00:01:49.079 --> 00:01:54.299
use a meta class to configure the
serializer to point to a specific model

00:01:54.299 --> 00:02:03.630
in our project so create a new meta
class by typing class meta and then in

00:02:03.630 --> 00:02:11.280
the class we define a new variable
called model so model equals models dot

00:02:11.280 --> 00:02:14.620
user profile

00:02:14.620 --> 00:02:17.459
this sets our serializer up to point to

00:02:17.459 --> 00:02:22.560
our user profile model the next thing
that you do with a model sterilizer is

00:02:22.560 --> 00:02:27.690
you need to specify a list of fields in
our model that we want to manage through

00:02:27.690 --> 00:02:33.060
our serializer so this is a list of all
the fields that you want to either make

00:02:33.060 --> 00:02:40.160
accessible in our API or you want to use
to create new models with our serializer

00:02:40.160 --> 00:02:45.299
so you define the list of fields by
typing fields and you pass in a tuple

00:02:45.299 --> 00:02:49.980
which is the same as a list but it uses these curved brackets instead of

00:02:49.980 --> 00:02:54.690
the square brackets and we're going to
define the list or the tuple of

00:02:54.690 --> 00:02:58.290
fields that we want to make accessible
in our model so we're going to use the

00:02:58.290 --> 00:03:07.560
ID field the email the name and the
password

00:03:07.560 --> 00:03:09.989
so this is the list of fields

00:03:09.989 --> 00:03:14.129
that we want to work with we want to
make an exception to the password field

00:03:14.129 --> 00:03:19.139
because we only want to use this when
creating new users in the system we

00:03:19.139 --> 00:03:22.859
don't want to allow the users to
retrieve the password hash because

00:03:22.859 --> 00:03:26.700
there's certain security risks
associated with that so we want to make

00:03:26.700 --> 00:03:33.150
this password field write only the way
you do that is you use the extra keyword

00:03:33.150 --> 00:03:41.579
args variable here so type extra
underscore kwargs equals and this

00:03:41.579 --> 00:03:46.980
is going to be a dictionary and the keys
of the dictionary are the fields that

00:03:46.980 --> 00:03:51.150
you want to add the custom configuration
to so we're only going to provide one

00:03:51.150 --> 00:03:57.989
for the password field and then create a
new dictionary associated with this

00:03:57.989 --> 00:04:03.569
password and we are going to add two
more key value pairs the first one is

00:04:03.569 --> 00:04:10.379
going to be write underscore only and
the value is going to be true so this

00:04:10.379 --> 00:04:15.720
says when we create our password field
from our model set it to write only

00:04:15.720 --> 00:04:21.449
equals true that means you can only use
it to create new objects or update

00:04:21.449 --> 00:04:26.490
objects you can't use it to retrieve
objects so when you do a get you won't

00:04:26.490 --> 00:04:29.780
see the password field included in that response

00:04:29.780 --> 00:04:34.979
the second thing we're going to do is
add a custom style to this and this is

00:04:34.979 --> 00:04:40.860
just for the browsable api and what it
does is it will set the field type to a

00:04:40.860 --> 00:04:45.539
password field which means you won't be
able to see the input as you're typing

00:04:45.539 --> 00:04:50.009
it so you'll just see the dots or the
stars that you would expect in a regular

00:04:50.009 --> 00:04:57.300
password input field so let's type style
: and then this accepts another

00:04:57.300 --> 00:05:04.800
dictionary and we're going to put input
underscore type and we're going to give

00:05:04.800 --> 00:05:09.020
it the input type of password

00:05:09.020 --> 00:05:12.840
Okay so that's the field and model configuration

00:05:12.840 --> 00:05:16.650
of our user profile steriliser the next
thing we're going to do is we're going

00:05:16.650 --> 00:05:22.050
to overwrite the create function by
default the model serializer allows you

00:05:22.050 --> 00:05:28.259
to create simple objects in the database
so it uses the default create function

00:05:28.259 --> 00:05:33.150
of the object manager to create the
object we want to override this

00:05:33.150 --> 00:05:38.130
functionality for this particular
serializer so that it uses the create

00:05:38.130 --> 00:05:43.800
user function instead of the create
function the reason we do this is so

00:05:43.800 --> 00:05:49.949
that the password gets created as a hash
and not the clear text password that it

00:05:49.949 --> 00:05:55.139
would do by default if we didn't
override the function the way you

00:05:55.139 --> 00:06:00.180
override a create function in a model
serializer is you simply define a new

00:06:00.180 --> 00:06:09.469
function in the class called def create
and pass in the argument self and

00:06:09.469 --> 00:06:14.880
validated data

00:06:14.880 --> 00:06:21.600
and give it the doc string create and return a new user

00:06:21.600 --> 00:06:28.319
Make sure that the function is four
characters or at one tab away from the

00:06:28.319 --> 00:06:32.580
start of the class because sometimes it
can be confusing when you have a meta

00:06:32.580 --> 00:06:35.430
class here you don't want to put the create function

00:06:35.430 --> 00:06:40.349
inside the meta class you want to make
sure it's within the serializer class

00:06:40.349 --> 00:06:44.220
here otherwise it won't work properly

00:06:44.220 --> 00:06:46.199
Okay so what happens here is whenever we

00:06:46.199 --> 00:06:50.009
create a new object with our user
profile serializer

00:06:50.009 --> 00:06:55.469
it will validate the object or validate
the fields provided to the serializer

00:06:55.469 --> 00:07:01.880
and then it will call this create
function passing in the validated data

00:07:01.880 --> 00:07:07.889
so what we want to do here is we want to
create and return a new user from our user

00:07:07.889 --> 00:07:17.460
profiles model manager so we'll type
user equals models dot user profile dot

00:07:17.460 --> 00:07:23.370
objects dot create underscore user and
then we're going to pass in the

00:07:23.370 --> 00:07:29.009
appropriate fields from the validated
data so we'll pass in email equals

00:07:29.009 --> 00:07:35.370
validated underscore data and we want to
retrieve the email field from the

00:07:35.370 --> 00:07:41.039
validated data and then we will pass in
name equals validated underscore data

00:07:41.039 --> 00:07:52.080
and we will get the name key then
password equals validated data password

00:07:52.080 --> 00:08:00.760
and then finally we will return the new
user

00:08:00.760 --> 00:08:02.370
okay so we can save that

00:08:02.370 --> 00:08:07.830
serializers file and what this does is
it will override the create function and

00:08:07.830 --> 00:08:13.770
call our create user function that we
previously defined here in our user

00:08:13.770 --> 00:08:19.169
profile manager so we use this function
to create new users in the database and

00:08:19.169 --> 00:08:24.870
as you can see this function calls the
set password to set the password instead

00:08:24.870 --> 00:08:29.160
of just passing all the keys in as plain
text

00:08:29.160 --> 00:08:31.199
Okay so that's how you create a

00:08:31.199 --> 00:08:35.959
serializer for our profiles API

