WEBVTT
Kind: captions
Language: en

00:00:00.030 --> 00:00:05.580
Now that we have our serializer we can
go ahead and create a view set for our

00:00:05.580 --> 00:00:13.380
profile feed items open up the views dot
py file and scroll right to the bottom

00:00:13.380 --> 00:00:23.220
and let's add a new class called class
user profile feed view set then we're

00:00:23.220 --> 00:00:28.740
going to base our class from view sets dot Model View set and we're going to add the

00:00:28.740 --> 00:00:39.750
doc string handles creating reading and
updating profile feed items okay so

00:00:39.750 --> 00:00:44.370
we're going to use the token
authentication to authenticate requests

00:00:44.370 --> 00:00:53.149
to our endpoints so type authentication
underscore classes equals token

00:00:53.149 --> 00:00:58.859
authentication and don't forget to add a comma at the end of token

00:00:58.859 --> 00:01:03.900
authentication so this gets passed in as
a tuple

00:01:03.900 --> 00:01:05.460
next we're going to set the

00:01:05.460 --> 00:01:09.330
serializer class to this serializer
that we created previously

00:01:09.330 --> 00:01:20.600
so type serializer underscore class
equals serializers dot profile feed item

00:01:20.600 --> 00:01:23.320
serializer

00:01:23.320 --> 00:01:26.130
so this sets the serializer class on our

00:01:26.130 --> 00:01:31.280
view set to the profile feed item
serializer

00:01:31.280 --> 00:01:32.700
next we're going to assign

00:01:32.700 --> 00:01:38.310
the query set that's going to be managed
through our view set

00:01:38.310 --> 00:01:49.290
so type queryset equals models dot
profile feed item dot objects dot all so

00:01:49.290 --> 00:01:54.770
we're going to manage all of our profile
feed item objects from our model in our

00:01:54.770 --> 00:01:57.400
view set

00:01:57.400 --> 00:02:02.369
ok so this sets up a basic model view set that allows us to create

00:02:02.369 --> 00:02:07.200
and manage feed item objects in the
database however as I explained

00:02:07.200 --> 00:02:11.970
previously we want to set this user
profile to read only

00:02:11.970 --> 00:02:16.680
because we're going to set it based on
the authenticated user in order to do

00:02:16.680 --> 00:02:24.300
that we need to add a perform create
function to our Model View set so type

00:02:24.300 --> 00:02:34.140
def perform underscore create self
serializer and then write in the doc

00:02:34.140 --> 00:02:42.980
string sets the user profile to the
logged in user

00:02:42.980 --> 00:02:44.340
the perform create

00:02:44.340 --> 00:02:49.350
function is a handy feature of the
Django rest framework that allows you to

00:02:49.350 --> 00:02:54.890
override the behavior or customize the
behavior for creating objects through a

00:02:54.890 --> 00:02:57.180
Model View set

00:02:57.180 --> 00:03:01.560
so when a request gets made to our view set it gets passed into

00:03:01.560 --> 00:03:08.880
our serializer class and validated and
then the serializer dot save function is

00:03:08.880 --> 00:03:11.260
called by default

00:03:11.260 --> 00:03:14.520
if we need to customize the logic for creating an

00:03:14.520 --> 00:03:20.640
object then we can do this using the
perform create function so this perform

00:03:20.640 --> 00:03:27.500
create function gets called every time
you do an HTTP POST to our view set

00:03:27.500 --> 00:03:35.550
let's set the user profile by typing
serializer dot save user underscore

00:03:35.550 --> 00:03:42.020
profile equals self dot request dot user

00:03:42.020 --> 00:03:43.560
so let me just explain a little bit

00:03:43.560 --> 00:03:48.030
about how this works
so when a new object is created Django

00:03:48.030 --> 00:03:53.070
rest framework calls perform create and
it passes in the serializer that we're

00:03:53.070 --> 00:03:59.700
using to create the object the
serializer is a model serializer so it

00:03:59.700 --> 00:04:05.040
has a save function assigned to it and
that save function is used to save the

00:04:05.040 --> 00:04:10.100
contents of the serializer to an object
in the database

00:04:10.100 --> 00:04:11.240
what we're doing here is

00:04:11.250 --> 00:04:16.410
we are calling serializer dot save and
we're passing in an additional keyword

00:04:16.410 --> 00:04:23.100
for the user profile this gets passed in
in addition to all of the items in the

00:04:23.100 --> 00:04:26.780
serializer that have been validated

00:04:26.780 --> 00:04:28.890
so we're going to set the user profile

00:04:28.890 --> 00:04:35.340
to this self dot request dot user the
request object is an object that gets

00:04:35.340 --> 00:04:41.610
passed into all view sets every time a
request is made and as the name suggests

00:04:41.610 --> 00:04:47.570
it contains all of the details about the
request being made to the view set

00:04:47.570 --> 00:04:53.070
because we've added the token
authentication to our view set if the

00:04:53.070 --> 00:04:59.760
user has authenticated then the request
will have a user associated to the

00:04:59.760 --> 00:05:05.280
authenticated user so this user field gets added whenever the user

00:05:05.280 --> 00:05:09.240
is authenticated if they're not
authenticated then it's just set to an

00:05:09.240 --> 00:05:12.360
anonymous user account

00:05:12.360 --> 00:05:15.480
okay so save views dot py and let's head

00:05:15.480 --> 00:05:23.250
over to the URLs and link up our new
view set to a URL below the profile the

00:05:23.250 --> 00:05:26.790
registering of the profile view set lets type router

00:05:26.790 --> 00:05:32.490
dot register and we're going to call
this URL feed because it's going to be

00:05:32.490 --> 00:05:39.680
the user's feed and we're going to
assign it to views dot user profile feed

00:05:39.680 --> 00:05:42.980
view set

00:05:42.980 --> 00:05:47.940
Okay so save the URLs dot py file
and that's how you set up a view set in

00:05:47.940 --> 00:05:51.140
the Django rest framework

