WEBVTT
Kind: captions
Language: en

00:00:00.030 --> 00:00:07.919
Next we're going to create a serializer
for our profile feed item objects open

00:00:07.919 --> 00:00:12.960
up the serializers dot py file and at
the bottom let's create a new class

00:00:12.960 --> 00:00:20.360
called class profile feed item
serializer

00:00:20.360 --> 00:00:21.689
we're going to base our class

00:00:21.689 --> 00:00:27.600
from serializers dot model serializer

00:00:27.600 --> 00:00:30.349
then in the doc string let's type

00:00:30.349 --> 00:00:37.980
serializes profile feed items and we're
going to set our model serializer to our

00:00:37.980 --> 00:00:48.030
profile feed item class so create class
meta model equals models dot profile

00:00:48.030 --> 00:00:51.200
feed item

00:00:51.200 --> 00:00:54.660
this sets our serializer or our model serializer

00:00:54.660 --> 00:01:02.400
to our profile feed item model that we
created in models dot py

00:01:02.400 --> 00:01:03.539
our profile feed

00:01:03.539 --> 00:01:09.150
item has three fields that we've added
here one is the user profile that is

00:01:09.150 --> 00:01:16.110
associated to the feed item the next one
is the status text and finally we have

00:01:16.110 --> 00:01:23.490
the created on so the user profile is
the profile that owns or created the

00:01:23.490 --> 00:01:28.439
profile feed item the status text will
be the content of the feed item so kind

00:01:28.439 --> 00:01:34.350
of like a status update and the created
on is automatically set to a date time

00:01:34.350 --> 00:01:40.020
field with the current time that the
item was created

00:01:40.020 --> 00:01:41.540
so we need to make

00:01:41.549 --> 00:01:46.780
these fields available through our
serializer

00:01:46.780 --> 00:01:50.310
type fields equals and I

00:01:50.310 --> 00:01:55.470
always like to include the ID so we can
use that to retrieve specific objects

00:01:55.470 --> 00:02:02.009
and by default Django adds a primary key
ID to all models that you create so the

00:02:02.009 --> 00:02:06.360
first one we're going to set is ID and
then we'll put the user underscore

00:02:06.360 --> 00:02:16.920
profile and then status underscore text
and then created underscore on so

00:02:16.920 --> 00:02:22.200
because the ID is set up by Django by
default it's automatically set to read

00:02:22.200 --> 00:02:27.840
only so when you create a new object the
new ID is created by the database and it is

00:02:27.840 --> 00:02:33.870
set to the next available integer field
or integer value in the table so just

00:02:33.870 --> 00:02:40.620
increases for every object you create
the created on is automatically set as

00:02:40.620 --> 00:02:43.980
well it's automatically set to the time
that it's created so that will by default

00:02:43.980 --> 00:02:50.880
be read-only so the only fields that
will be writable in our serializer in

00:02:50.880 --> 00:02:56.640
its current state are the user profile
and the status text however we don't

00:02:56.640 --> 00:03:00.810
want the users to be able to set the
user profile when they create a new feed

00:03:00.810 --> 00:03:06.150
item we want to set the user profile
based on the user that is authenticated

00:03:06.150 --> 00:03:10.860
so I don't want to be able to create...
or I don't want one user to be able to

00:03:10.860 --> 00:03:15.150
create a new profile feed item and
assign that to another user because that

00:03:15.150 --> 00:03:19.350
would be a security flaw in the system
so we want to set this to the

00:03:19.350 --> 00:03:23.610
authenticated user and therefore we're
going to make the user profile field

00:03:23.610 --> 00:03:25.540
read-only

00:03:25.540 --> 00:03:29.520
so that means that when we list the objects we can see which users

00:03:29.520 --> 00:03:34.170
created which feed items but when we
create an object it can only be assigned

00:03:34.170 --> 00:03:38.860
to the current user that is authenticated

00:03:38.860 --> 00:03:40.590
so below the fields we're going to set a

00:03:40.590 --> 00:03:48.450
new value called extra underscore kwargs equals and then we set this to a

00:03:48.450 --> 00:03:53.370
dictionary and this is how you add extra
keyword arguments to fields and we're

00:03:53.370 --> 00:03:59.310
going to set extra arguments to the user underscore profile field and the

00:03:59.310 --> 00:04:03.450
arguments we're going to set are read
underscore only and we're going to set that

00:04:03.450 --> 00:04:10.410
to true so this allows us to set the
user profile field to read-only in our

00:04:10.410 --> 00:04:13.200
model serializer

00:04:13.200 --> 00:04:15.840
okay so save the file and that's all we

00:04:15.840 --> 00:04:21.680
need to do to create a serializer for
our profile feed item objects

