Now that we have our serializer we can go ahead and create a view set for our
profile feed items open up the views dot py file and scroll right to the bottom
and let's add a new class called class user profile feed view set then we're
going to base our class from view sets dot Model View set and we're going to add the
doc string handles creating reading and updating profile feed items okay so
we're going to use the token authentication to authenticate requests
to our endpoints so type authentication underscore classes equals token
authentication and don't forget to add a comma at the end of token
authentication so this gets passed in as a tuple
next we're going to set the
serializer class to this serializer that we created previously
so type serializer underscore class equals serializers dot profile feed item
serializer
so this sets the serializer class on our
view set to the profile feed item serializer
next we're going to assign
the query set that's going to be managed through our view set
so type queryset equals models dot profile feed item dot objects dot all so
we're going to manage all of our profile feed item objects from our model in our
view set
ok so this sets up a basic model view set that allows us to create
and manage feed item objects in the database however as I explained
previously we want to set this user profile to read only
because we're going to set it based on the authenticated user in order to do
that we need to add a perform create function to our Model View set so type
def perform underscore create self serializer and then write in the doc
string sets the user profile to the logged in user
the perform create
function is a handy feature of the Django rest framework that allows you to
override the behavior or customize the behavior for creating objects through a
Model View set
so when a request gets made to our view set it gets passed into
our serializer class and validated and then the serializer dot save function is
called by default
if we need to customize the logic for creating an
object then we can do this using the perform create function so this perform
create function gets called every time you do an HTTP POST to our view set
let's set the user profile by typing serializer dot save user underscore
profile equals self dot request dot user
so let me just explain a little bit
about how this works so when a new object is created Django
rest framework calls perform create and it passes in the serializer that we're
using to create the object the serializer is a model serializer so it
has a save function assigned to it and that save function is used to save the
contents of the serializer to an object in the database
what we're doing here is
we are calling serializer dot save and we're passing in an additional keyword
for the user profile this gets passed in in addition to all of the items in the
serializer that have been validated
so we're going to set the user profile
to this self dot request dot user the request object is an object that gets
passed into all view sets every time a request is made and as the name suggests
it contains all of the details about the request being made to the view set
because we've added the token authentication to our view set if the
user has authenticated then the request will have a user associated to the
authenticated user so this user field gets added whenever the user
is authenticated if they're not authenticated then it's just set to an
anonymous user account
okay so save views dot py and let's head
over to the URLs and link up our new view set to a URL below the profile the
registering of the profile view set lets type router
dot register and we're going to call this URL feed because it's going to be
the user's feed and we're going to assign it to views dot user profile feed
view set
Okay so save the URLs dot py file and that's how you set up a view set in
the Django rest framework