WEBVTT
Kind: captions
Language: en

00:00:00.060 --> 00:00:05.819
The first step of creating our user
profile feed API is to create a new

00:00:05.819 --> 00:00:12.960
Django model for storing the user
profile feed items in the database so

00:00:12.960 --> 00:00:20.490
open up the project in the atom editor
and open up the models dot py file we're

00:00:20.490 --> 00:00:24.119
going to start by adding a new import
into the top of the file and that's

00:00:24.119 --> 00:00:31.170
going to be the settings import from the
Django dot conf module so type from Django

00:00:31.170 --> 00:00:39.239
dot conf import settings this is used to
retrieve settings from our settings dot

00:00:39.239 --> 00:00:46.829
py file in the project settings of our
Django project the particular setting

00:00:46.829 --> 00:00:49.829
that we're going to retrieve is this
author user model and I'm going to show

00:00:49.829 --> 00:00:54.690
you why we need to retrieve that in a
moment so at the bottom of our models

00:00:54.690 --> 00:01:02.010
dot py file let's create a new model and
we're going to call our model class profile

00:01:02.010 --> 00:01:10.670
feed item and we're going to base it from
models dot model and at the dock string

00:01:10.670 --> 00:01:18.150
profile status update so this is going
to be the model we use to allow users to

00:01:18.150 --> 00:01:24.240
store status updates in the system so
every time they create a new update it's

00:01:24.240 --> 00:01:29.310
going to create a new profile feed item
object and associate that object with

00:01:29.310 --> 00:01:35.549
the user that created it the way you
link models to other models in Django is

00:01:35.549 --> 00:01:40.979
you use what's called a foreign key when
you use the foreign key field it sets up

00:01:40.980 --> 00:01:46.740
a foreign key relationship in the
database to a remote model

00:01:46.740 --> 00:01:53.100
the benefit of doing this is that it
allows you to ensure that the integrity

00:01:53.100 --> 00:01:57.810
of the database is maintained so you can
never create a profile feed item for a

00:01:57.810 --> 00:02:04.409
user profile that doesn't exist so let's
go ahead and add our user profile field

00:02:04.409 --> 00:02:11.940
to our profile feed item type user
underscore profile equals models dot

00:02:11.940 --> 00:02:16.969
foreign key
and the first argument of a foreign key

00:02:16.969 --> 00:02:22.700
field in models is the name of the model
that is the remote model for this

00:02:22.700 --> 00:02:28.219
foreign key so which model do we want to
set a relationship up from this user

00:02:28.219 --> 00:02:33.640
profile field to a remote model so
we're going to set it up to the user

00:02:33.640 --> 00:02:39.920
profile model now you could specify this
as a string as the first argument of the

00:02:39.920 --> 00:02:45.469
foreign key however when you're
referencing the auth user model in

00:02:45.469 --> 00:02:50.420
Django it's best practice to retrieve
this from the settings dot py

00:02:50.420 --> 00:02:55.010
configuration the reason for this is
that you may decide that you want to

00:02:55.010 --> 00:03:00.200
switch out this user profile model as
our default authentication model and you

00:03:00.200 --> 00:03:04.639
want to switch it back to the Django
default model if you hard-coded the

00:03:04.639 --> 00:03:08.780
name of the model here then you would
have to go through and manually update

00:03:08.780 --> 00:03:12.560
all of the foreign keys where you
hard-coded that if you were to switch

00:03:12.560 --> 00:03:15.340
out the default user model

00:03:15.340 --> 00:03:19.000
so what we do instead is we've reference it by typing

00:03:19.000 --> 00:03:24.900
settings dot auth user model

00:03:24.900 --> 00:03:28.190
and what this will do is it will retrieve the value

00:03:28.190 --> 00:03:33.680
from the author user model setting in
our settings dot py file so if we ever

00:03:33.680 --> 00:03:37.669
swap this auth user model to a
different model then the relationships

00:03:37.669 --> 00:03:42.199
will automatically be updated without us
having to go through and manually change

00:03:42.199 --> 00:03:48.319
it everywhere that we've referenced it
in our models dot py file ok so the

00:03:48.319 --> 00:03:54.430
second argument we need to provide is
the on delete argument so type on delete

00:03:54.430 --> 00:03:58.400
equals models dot cascade

00:03:58.400 --> 00:04:02.150
now what the on delete does is it tells Django or it

00:04:02.150 --> 00:04:09.229
tells the database what to do if the
remote field is deleted so we have user

00:04:09.229 --> 00:04:14.060
we have profile feed items in our
database and each one of them has a user

00:04:14.060 --> 00:04:20.329
profile associated with it now because
there's a foreign key Association the

00:04:20.329 --> 00:04:25.490
database needs to know what happens if
you remove a user profile what should

00:04:25.490 --> 00:04:30.000
happen to the profile feed items
that are associated with it and the way you

00:04:30.000 --> 00:04:34.229
do that is by specifying this on delete
so there are different things that you

00:04:34.229 --> 00:04:39.150
can set on delete one of them is cascade
and what that does is it says basically

00:04:39.150 --> 00:04:43.590
cascade the changes down through all the
related fields so if you have user

00:04:43.590 --> 00:04:48.810
profile feed items associated to a user
profile and you remove that profile then

00:04:48.810 --> 00:04:53.610
it will cascade the change down and
remove the associated feed items from

00:04:53.610 --> 00:04:54.880
that user

00:04:54.880 --> 00:04:59.550
another option is to set that
field as null so what it would do is

00:04:59.550 --> 00:05:04.889
models dot set null would set the value
of the user profile to null if the

00:05:04.889 --> 00:05:07.900
remote user profile is deleted

00:05:07.900 --> 00:05:09.960
now I think it's better to cascade in this

00:05:09.960 --> 00:05:13.620
case because if a user wants to delete
their profile then we can assume that

00:05:13.620 --> 00:05:16.830
they also want to delete all the feed
items that are associated with their

00:05:16.830 --> 00:05:18.500
profile

00:05:18.500 --> 00:05:22.770
alright so the next field that
we're going to add is the status text

00:05:22.770 --> 00:05:28.409
and the status text is going to be used
to contain the text of the feed update

00:05:28.409 --> 00:05:34.620
so type status underscore text equals
models dot

00:05:34.620 --> 00:05:40.920
char field max length equals 255

00:05:40.920 --> 00:05:42.659
so we're going to set a char field here which

00:05:42.659 --> 00:05:47.909
is a standard character field and allows
us to put in a text input up to 255

00:05:47.909 --> 00:05:50.120
characters long

00:05:50.120 --> 00:05:54.389
next we're going to add a created on field so type created

00:05:54.389 --> 00:06:02.370
underscore on equals models dot
date time field and then set the

00:06:02.370 --> 00:06:09.419
argument auto now add equals true and
what this does is it says every time we

00:06:09.419 --> 00:06:15.060
create a new feed item automatically add
the date time stamp that the item was

00:06:15.060 --> 00:06:18.539
created so we don't need to manually set
this when we're creating the item it will

00:06:18.539 --> 00:06:26.000
automatically be set to the current time
because of this auto now add parameter

00:06:26.000 --> 00:06:31.560
okay finally we're going to add a string
representation of our model so that is

00:06:31.560 --> 00:06:36.810
to tell Python what to do when we
convert a model instance into a string

00:06:36.810 --> 00:06:40.600
so we're going to type def underscore
underscore

00:06:40.600 --> 00:06:48.250
str underscore underscore open brackets
self :

00:06:48.250 --> 00:06:54.040
and we'll write the doc string return
the model as a string and then we'll

00:06:54.040 --> 00:07:00.640
just do return self dot status
underscore text so when we convert this

00:07:00.640 --> 00:07:06.580
to a string we want to see the status
text value that is associated to the

00:07:06.580 --> 00:07:07.820
model

00:07:07.820 --> 00:07:12.250
okay so that's how you create a
profile feed item model in django make

00:07:12.250 --> 00:07:17.460
sure you save the file and i'll see you
in the next video

