Next we're going to create a serializer for our profile feed item objects open
up the serializers dot py file and at the bottom let's create a new class
called class profile feed item serializer
we're going to base our class
from serializers dot model serializer
then in the doc string let's type
serializes profile feed items and we're going to set our model serializer to our
profile feed item class so create class meta model equals models dot profile
feed item
this sets our serializer or our model serializer
to our profile feed item model that we created in models dot py
our profile feed
item has three fields that we've added here one is the user profile that is
associated to the feed item the next one is the status text and finally we have
the created on so the user profile is the profile that owns or created the
profile feed item the status text will be the content of the feed item so kind
of like a status update and the created on is automatically set to a date time
field with the current time that the item was created
so we need to make
these fields available through our serializer
type fields equals and I
always like to include the ID so we can use that to retrieve specific objects
and by default Django adds a primary key ID to all models that you create so the
first one we're going to set is ID and then we'll put the user underscore
profile and then status underscore text and then created underscore on so
because the ID is set up by Django by default it's automatically set to read
only so when you create a new object the new ID is created by the database and it is
set to the next available integer field or integer value in the table so just
increases for every object you create the created on is automatically set as
well it's automatically set to the time that it's created so that will by default
be read-only so the only fields that will be writable in our serializer in
its current state are the user profile and the status text however we don't
want the users to be able to set the user profile when they create a new feed
item we want to set the user profile based on the user that is authenticated
so I don't want to be able to create... or I don't want one user to be able to
create a new profile feed item and assign that to another user because that
would be a security flaw in the system so we want to set this to the
authenticated user and therefore we're going to make the user profile field
read-only
so that means that when we list the objects we can see which users
created which feed items but when we create an object it can only be assigned
to the current user that is authenticated
so below the fields we're going to set a
new value called extra underscore kwargs equals and then we set this to a
dictionary and this is how you add extra keyword arguments to fields and we're
going to set extra arguments to the user underscore profile field and the
arguments we're going to set are read underscore only and we're going to set that
to true so this allows us to set the user profile field to read-only in our
model serializer
okay so save the file and that's all we
need to do to create a serializer for our profile feed item objects