The first step of creating our user profile feed API is to create a new
Django model for storing the user profile feed items in the database so
open up the project in the atom editor and open up the models dot py file we're
going to start by adding a new import into the top of the file and that's
going to be the settings import from the Django dot conf module so type from Django
dot conf import settings this is used to retrieve settings from our settings dot
py file in the project settings of our Django project the particular setting
that we're going to retrieve is this author user model and I'm going to show
you why we need to retrieve that in a moment so at the bottom of our models
dot py file let's create a new model and we're going to call our model class profile
feed item and we're going to base it from models dot model and at the dock string
profile status update so this is going to be the model we use to allow users to
store status updates in the system so every time they create a new update it's
going to create a new profile feed item object and associate that object with
the user that created it the way you link models to other models in Django is
you use what's called a foreign key when you use the foreign key field it sets up
a foreign key relationship in the database to a remote model
the benefit of doing this is that it allows you to ensure that the integrity
of the database is maintained so you can never create a profile feed item for a
user profile that doesn't exist so let's go ahead and add our user profile field
to our profile feed item type user underscore profile equals models dot
foreign key and the first argument of a foreign key
field in models is the name of the model that is the remote model for this
foreign key so which model do we want to set a relationship up from this user
profile field to a remote model so we're going to set it up to the user
profile model now you could specify this as a string as the first argument of the
foreign key however when you're referencing the auth user model in
Django it's best practice to retrieve this from the settings dot py
configuration the reason for this is that you may decide that you want to
switch out this user profile model as our default authentication model and you
want to switch it back to the Django default model if you hard-coded the
name of the model here then you would have to go through and manually update
all of the foreign keys where you hard-coded that if you were to switch
out the default user model
so what we do instead is we've reference it by typing
settings dot auth user model
and what this will do is it will retrieve the value
from the author user model setting in our settings dot py file so if we ever
swap this auth user model to a different model then the relationships
will automatically be updated without us having to go through and manually change
it everywhere that we've referenced it in our models dot py file ok so the
second argument we need to provide is the on delete argument so type on delete
equals models dot cascade
now what the on delete does is it tells Django or it
tells the database what to do if the remote field is deleted so we have user
we have profile feed items in our database and each one of them has a user
profile associated with it now because there's a foreign key Association the
database needs to know what happens if you remove a user profile what should
happen to the profile feed items that are associated with it and the way you
do that is by specifying this on delete so there are different things that you
can set on delete one of them is cascade and what that does is it says basically
cascade the changes down through all the related fields so if you have user
profile feed items associated to a user profile and you remove that profile then
it will cascade the change down and remove the associated feed items from
that user
another option is to set that field as null so what it would do is
models dot set null would set the value of the user profile to null if the
remote user profile is deleted
now I think it's better to cascade in this
case because if a user wants to delete their profile then we can assume that
they also want to delete all the feed items that are associated with their
profile
alright so the next field that we're going to add is the status text
and the status text is going to be used to contain the text of the feed update
so type status underscore text equals models dot
char field max length equals 255
so we're going to set a char field here which
is a standard character field and allows us to put in a text input up to 255
characters long
next we're going to add a created on field so type created
underscore on equals models dot date time field and then set the
argument auto now add equals true and what this does is it says every time we
create a new feed item automatically add the date time stamp that the item was
created so we don't need to manually set this when we're creating the item it will
automatically be set to the current time because of this auto now add parameter
okay finally we're going to add a string representation of our model so that is
to tell Python what to do when we convert a model instance into a string
so we're going to type def underscore underscore
str underscore underscore open brackets self :
and we'll write the doc string return the model as a string and then we'll
just do return self dot status underscore text so when we convert this
to a string we want to see the status text value that is associated to the
model
okay so that's how you create a profile feed item model in django make
sure you save the file and i'll see you in the next video