Hello everyone and welcome to this new lesson about view sets a ruthless view said classes allow us
to combine the logic for a set of related views in a single class.
If you set could for example allow us to get a list of elements from a query set but also allow us to
get the details of a single instance of a same model views at work at the earliest abstraction level
compared to all the EPA views that we've learned to use.
So far they are really powerful and I am pretty sure that you're going to love them.
Few sites are in fact another kind of class based view that doesn't provide any method then let us such
as Get or both and instead provides action methods such as at least and create like some other classes.
We already know we will discuss and understand how and why.
By analyzing data source code view sets are typically used in combination with the router class allowing
us to automatically get a real path configuration that is appropriate to the different kind of actions
that if you said provides following convention standards so tend to code.
Let's see what we are really talking about.
We are in business with your code.
As you can see I'm currently weaving the views dot by file of the API for that weaving their profile
SAP.
And yeah we've got our profile least class that of course provides us at least we have all the profiles
in our database.
So far we've come to understand our really powerful these kind of classes are.
It's enough to write just very few lines of code to get a really powerful behavior out of it.
And we also know that if we wanted to create another endpoint for our arrest API and endpoint maybe
to get the details of a single profile instance we would have to create yet another class a profile
detail class where most of the attributes would be the same using view sets however everything gets
much easier.
As we said they allow us to combine the logic for a set of related views in a single class allowing
us to be the different end points for the same model without having to write all their code which is
provided by the abusive class instead.
This is great because it allows us to keep our code much cleaner and maintainable than it would otherwise
be without having to write the same code multiple times in the same file.
So let's set up if you said class to get at the same time both at least and detail end points.
So first of all I'm going to import a read only view set from this framework that you set import read
only model view set.
Yeah we can change generics at least EPA view we've read only model view set.
We can also change the name.
So instead of being part of a least there's going to be a profile view set and everything else stays
the same.
So before checking the code of the read only model view said class.
Let's go to the US that by file because there is a bit of a special configuration that we need to do.
First of all clearly we need to import the view said class.
And even though the class provides all the code we need for free we still need to specify which kind
of endpoint we want to build we want profile at least because we want to provide a list with all the
profile instances in our database and so we do profile view set dot s view.
And of course we've used as view for a profile list as well.
But this time we pass a dictionary where we specify the HDP verb getter and the action list we can now
specify.
Profile detail which is going to be provided you said Dot is view by this time using get want to retrieve
the details of a single instance we are now ready to define our end points.
I'm going to substitute profile least of his view with a profile least using the profile view set the
name stays the same.
And now I can't define the second endpoint but we can define profiles integer primary key this time
using profile detailed you name it is going to be profile detail we use integer primary key to get the
details of a single profile instance because considering that this is a programming convention in Django
it's a standard that you set the class clearly is the set in a way that allows us to use the same standard.
I see that there is a small mistake here need to change changes next so because it's a dictionary clearly
and we're now ready to test that to you end point.
So let's restart our development server like so and let's go to Chrome.
So let's go to slash API slash profiles.
Clearly we need to log into like so hand entities or at least we have all the profiles in our database.
Let's try to get the details of a single one for example arrest tester so many key three had the.
Awesome.
So how is this possible.
How can we get two different end points out of the same class.
Well it's time to dive into its code.
So going back to the view stood by file.
Let's now check the read only model view set code.
Go to definition so you'll see if you set that provides default list and retrieve actions.
We see it uses to make sense retrieve model make mixing and least model Maxine and then you can erase
from generic view set and the generic view set is right here above and we see that eating everything
from view set mixing and generic behavior that we already know.
So let's check the use of the mixing which is in the same fine up here and as you can read the docs
string tells it all.
This is the magic.
So how does it work.
Well we can really hold the rights as view so that it takes and actions keyword that performs the binding
of the TTP methods to actions on the resource for example to create a concrete view binding to get and
both methods to the least and create actions we can use as view as we did here in the year arrested
by file telling needs that we want to use getter to actually get a list out of the query set we're using
deduce it we've and then we want to bind create to post.
So in case you're curious about our with the s view method really works.
You can just cut me at and check his code so let's all go back to did you address that by file because
we have said that typically you will want to use the router class to create automatically all the different
endpoints for your view set using Convention standards.
Like for example using the primary key to get the details of a single profile instance so we can use
that with a class like so.
First of all we need to imported from risk framework dot routers import default a router then yeah we
can just delete these two and boys which are going to be generated automatically and the same fee for
the two views here.
We need to initialize the router instead.
So a router equals default router and we can now use this router instance to register get point that
we want to use.
So a router that register and so we want to register the profiles and point was going to be a rostering.
So I'm going to place an R in front of it and end here.
I'm going to pass a profile.
Use it like so.
So the router will automatically generate the links the different end points for us and therefore we
can include them within that EU URL path at least.
Like if they were part of a completely different app so path.
Include.
Which you also need to import of course.
And so here we can pass through the thought you address.
Let's now restart the development server.
Let's go back to Chrome and let's check the endpoints once again.
So I'm just going to guess the difference of the same instance we see that we get the details and the
name is changed.
Now it's profile instance and we also have a profile list.
Which of course does exactly what you would expect.
Not only that we now also have API road and we can read it provides us the default basic route view
for default router and we got a link to the main endpoint profile list.
So going back to visit your code once again there's no CEO to make a profile of view said even more
powerful in order for us to be able to also update this single instances all from the same as you said
glass.
And so in order to do so we're going to need two things.
First of all the genetic view sets glass and then all the mixing is related to the behavior that we
want to provide.
We no longer need read only more if you said I can just commented out.
We're going to need few settings that so for on the rest framework.
Import view sets and as we said sense.
So from rest framework import mixes and now here.
First of all make sense.
Don't update model.
Maxine then exceeds that list model Maxine.
Maxine is not retrieve more that Maxine.
And of course you said that generic view set.
We know that thanks to prophecy that Isaac except for the Avatar field we can update all the other fields
in a profile instance because of that we want to be sure of course that to instances can be updated
only by their own.
So basically we need to define a specific permission class let's create a new file within the EPA folder
admissions that by and of course we need to import permissions so from rest framework the import permissions
and we could create our class so class up his own profile or read only which clearly extends admissions
that base permission and then yeah.
Def as object permission but we do not need this code instead.
First of all we want to check if their request method is in permissions dot safe methods.
So if a request that not method in permissions that safe methods ended case we return True right away.
Otherwise the return object not user equals request that user.
And that's because as you probably remember in the profile model we've defined user as Model S not one
to one field to user.
Which of course we're using to authenticate our users therefore request not user can be compared to
the user instance we can now import the class right away.
We've seen that use that by file.
So from profile stored API dot permissions import is on profile ordinary don't leave we can just as
well switch position like so and we can now add the new class to the permission classes list.
So let's test it.
Let's restart the server once again and let's go back to cruel.
So I'm currently logged in as admin try to update the profile with Ida 3 instead so so as you can see
of course the end point accepts put and patch up but no update forum is shown because this is not our
profile.
Let's try to update our profile instead.
One.
And as you can see now of course we can update both biography and city.
For example I could write something like REST API administrator put and edit is perfect.
So it's finally time to write some code to manage the second point in our profile sop up the profile
status model and to do so.
We're going to use another if you set that model view set which is so powerful that provides us full
crowd functionality without having to use the mixing classes.
We also need to import both the model profit status and dissident eyes.
So profile start was nicer and we kind of defined our new of class so class up profile start to view
set which is going to extend the view set and we're going to need all these attributes like so.
But this time of course it's going to be provide start with the objects at all in the asset class is
going to provide start to see analyzer.
We also need to create a new mission class so so because of course using profile start to view set which
extends model view said we are going to be able to read update and delete the single profile start to
see instances.
Therefore the most powerful capabilities update and delete must be granted exclusively to the instance
owners.
So let's go back to permissions.
And here we can just call it is owner or if it only in the area we can refer object to user profile
equals request dot user not profile.
Actually pretty simple.
So let's go back to use that by file let's import permissions and let's change the permission classes
attribute like so now considering that we want to automatically connect the new profile start to see
instance to the profile of the user that is making the request we now have to override the perform create
method.
So DFA therefore create which is always accepts self and see nicer.
And so here are user profile equals self not requested the accuser both profile and then utilize her.
Let's say we have user profile equals user profile.
And of course this happens because the model of you said.
As you can see is composed by all the different mixes.
So let's now go back to the has stood by file because of course we need to register the new view set.
So profile status you set and so here you can register Stathis they need to provide that as you said.
So let's restart our development server once again.
Let's go back to Chrome you can actually go to the EPA route and you see that now we got.
So let's create our first stop was that was content hell a world post and you see user profile ad means
that there's content creator dead and updated that we can go back to profiles.
That was Lisa we clearly only have one.
So let's create another one second status post if we wanted to we could now update em like so for example
adding some more exclamation marks then put two and it it is updated and you see that we also have the
delete button.
But let's now test have another user so log out and let's log making as rest.
Test
like so.
And of course you see we now see the details of this specific statues but we can't neither updated or
deleted.
So as you can see Vue said routers are really really powerful.
And even though they might be working on a very high abstraction level always remember to check their
source code and generous frameworks documentation if you need specific details.
There are however some projects where using both view said glasses and concrete the view glasses is
the best design choice possible.
For example the specifications for our project required us to create an endpoint to let our users update
data about that.
That's why we've created this specific serialized it.
So in this case it's much better to use a genetic update EPA view class to build the endpoint.
So you're class.
How about that update view which is going to extend genetics not update EPA view we need of course to
import.
So profile about that.
Utilize it.
Yeah.
We can just so two and three we feel it to pull also like so.
So here we can pass serialize that class and remedial classes like so and is indicated is enough.
We do not need to define what he said because we are now going to other right.
D Get object mattered so that we can have an endpoint in the form of API is Lesh Avatar where there
is no need to pass any primary key because we are going to use the get object method to automatically
identify and return to the profile instance associated with request don't use it so that that specific
profile instance can be used by profile etc. either to update the avatar field.
So it's pretty easy.
We can define profile object as self though to request the user profile and then we can just return
profile object.
So let's not define the API is less about the endpoint in the understood by phase.
So first of all we need to import the view and I can use a tuple here just as well.
So or maybe just want to and then here we can just define it as any other end point.
So how about that who's going to call out that update view dot s view.
Name equals habitat update.
Going back to the views that I find that is one last feeling that we need to update that to realize
that a glacier like so so let's restart our development server and let's go back to Chrome.
First of all we need to look at
will look Dean as the rest.
Test.
Let's go to API slash Avatar.
We can select an avatar for our arrest test profile.
And yeah I can just select the python logo so put it.
Here is our avatar.
Let's go to the profiles list so API root profiles and you'll see that the admin user still as a red
square as an avatar.
Random still less no Avatar.
And of course again rest the test.
A python logo.
Okay so that was it for this lecture about the view said Andrew router classes in the next lecture.
We're going to talk about filtering in January's framework.