WEBVTT 00:00.120 --> 00:05.790 Hello everyone and welcome to this new lesson about view sets a ruthless view said classes allow us 00:05.790 --> 00:09.430 to combine the logic for a set of related views in a single class. 00:09.480 --> 00:15.150 If you set could for example allow us to get a list of elements from a query set but also allow us to 00:15.150 --> 00:20.730 get the details of a single instance of a same model views at work at the earliest abstraction level 00:20.760 --> 00:24.290 compared to all the EPA views that we've learned to use. 00:24.330 --> 00:28.990 So far they are really powerful and I am pretty sure that you're going to love them. 00:29.040 --> 00:35.250 Few sites are in fact another kind of class based view that doesn't provide any method then let us such 00:35.250 --> 00:43.210 as Get or both and instead provides action methods such as at least and create like some other classes. 00:43.240 --> 00:47.280 We already know we will discuss and understand how and why. 00:47.350 --> 00:53.580 By analyzing data source code view sets are typically used in combination with the router class allowing 00:53.580 --> 00:59.730 us to automatically get a real path configuration that is appropriate to the different kind of actions 00:59.870 --> 01:05.160 that if you said provides following convention standards so tend to code. 01:05.220 --> 01:07.280 Let's see what we are really talking about. 01:07.320 --> 01:08.730 We are in business with your code. 01:08.730 --> 01:13.730 As you can see I'm currently weaving the views dot by file of the API for that weaving their profile 01:13.730 --> 01:14.430 SAP. 01:14.640 --> 01:21.150 And yeah we've got our profile least class that of course provides us at least we have all the profiles 01:21.210 --> 01:22.880 in our database. 01:22.890 --> 01:27.170 So far we've come to understand our really powerful these kind of classes are. 01:27.350 --> 01:32.200 It's enough to write just very few lines of code to get a really powerful behavior out of it. 01:32.490 --> 01:38.280 And we also know that if we wanted to create another endpoint for our arrest API and endpoint maybe 01:38.280 --> 01:44.190 to get the details of a single profile instance we would have to create yet another class a profile 01:44.250 --> 01:50.820 detail class where most of the attributes would be the same using view sets however everything gets 01:50.820 --> 01:52.030 much easier. 01:52.110 --> 01:58.260 As we said they allow us to combine the logic for a set of related views in a single class allowing 01:58.260 --> 02:03.420 us to be the different end points for the same model without having to write all their code which is 02:03.420 --> 02:05.430 provided by the abusive class instead. 02:05.430 --> 02:10.500 This is great because it allows us to keep our code much cleaner and maintainable than it would otherwise 02:10.500 --> 02:14.680 be without having to write the same code multiple times in the same file. 02:14.700 --> 02:19.860 So let's set up if you said class to get at the same time both at least and detail end points. 02:19.860 --> 02:29.280 So first of all I'm going to import a read only view set from this framework that you set import read 02:29.340 --> 02:31.060 only model view set. 02:31.080 --> 02:36.530 Yeah we can change generics at least EPA view we've read only model view set. 02:36.540 --> 02:38.220 We can also change the name. 02:38.220 --> 02:44.370 So instead of being part of a least there's going to be a profile view set and everything else stays 02:44.370 --> 02:45.080 the same. 02:45.090 --> 02:48.050 So before checking the code of the read only model view said class. 02:48.420 --> 02:53.390 Let's go to the US that by file because there is a bit of a special configuration that we need to do. 02:53.490 --> 02:57.260 First of all clearly we need to import the view said class. 02:57.420 --> 03:03.300 And even though the class provides all the code we need for free we still need to specify which kind 03:03.300 --> 03:11.370 of endpoint we want to build we want profile at least because we want to provide a list with all the 03:11.640 --> 03:17.270 profile instances in our database and so we do profile view set dot s view. 03:17.580 --> 03:21.210 And of course we've used as view for a profile list as well. 03:21.240 --> 03:30.780 But this time we pass a dictionary where we specify the HDP verb getter and the action list we can now 03:31.050 --> 03:32.020 specify. 03:33.170 --> 03:43.090 Profile detail which is going to be provided you said Dot is view by this time using get want to retrieve 03:44.170 --> 03:48.360 the details of a single instance we are now ready to define our end points. 03:48.370 --> 03:54.070 I'm going to substitute profile least of his view with a profile least using the profile view set the 03:54.070 --> 03:55.890 name stays the same. 03:55.900 --> 04:04.540 And now I can't define the second endpoint but we can define profiles integer primary key this time 04:05.200 --> 04:12.250 using profile detailed you name it is going to be profile detail we use integer primary key to get the 04:12.250 --> 04:18.280 details of a single profile instance because considering that this is a programming convention in Django 04:18.310 --> 04:24.070 it's a standard that you set the class clearly is the set in a way that allows us to use the same standard. 04:24.400 --> 04:31.420 I see that there is a small mistake here need to change changes next so because it's a dictionary clearly 04:31.900 --> 04:34.550 and we're now ready to test that to you end point. 04:34.570 --> 04:41.030 So let's restart our development server like so and let's go to Chrome. 04:41.790 --> 04:47.800 So let's go to slash API slash profiles. 04:47.910 --> 04:57.030 Clearly we need to log into like so hand entities or at least we have all the profiles in our database. 04:57.040 --> 05:04.110 Let's try to get the details of a single one for example arrest tester so many key three had the. 05:04.510 --> 05:04.900 Awesome. 05:05.260 --> 05:06.820 So how is this possible. 05:06.820 --> 05:11.100 How can we get two different end points out of the same class. 05:11.110 --> 05:13.180 Well it's time to dive into its code. 05:13.180 --> 05:16.120 So going back to the view stood by file. 05:16.120 --> 05:18.890 Let's now check the read only model view set code. 05:19.090 --> 05:24.960 Go to definition so you'll see if you set that provides default list and retrieve actions. 05:24.970 --> 05:30.810 We see it uses to make sense retrieve model make mixing and least model Maxine and then you can erase 05:30.820 --> 05:36.560 from generic view set and the generic view set is right here above and we see that eating everything 05:36.640 --> 05:40.570 from view set mixing and generic behavior that we already know. 05:40.720 --> 05:48.170 So let's check the use of the mixing which is in the same fine up here and as you can read the docs 05:48.170 --> 05:49.790 string tells it all. 05:49.870 --> 05:50.710 This is the magic. 05:51.010 --> 05:52.480 So how does it work. 05:52.490 --> 06:00.820 Well we can really hold the rights as view so that it takes and actions keyword that performs the binding 06:00.850 --> 06:08.020 of the TTP methods to actions on the resource for example to create a concrete view binding to get and 06:08.020 --> 06:16.150 both methods to the least and create actions we can use as view as we did here in the year arrested 06:16.150 --> 06:23.270 by file telling needs that we want to use getter to actually get a list out of the query set we're using 06:23.300 --> 06:28.300 deduce it we've and then we want to bind create to post. 06:28.300 --> 06:32.740 So in case you're curious about our with the s view method really works. 06:32.740 --> 06:40.480 You can just cut me at and check his code so let's all go back to did you address that by file because 06:40.570 --> 06:45.920 we have said that typically you will want to use the router class to create automatically all the different 06:45.920 --> 06:50.010 endpoints for your view set using Convention standards. 06:50.050 --> 06:55.510 Like for example using the primary key to get the details of a single profile instance so we can use 06:55.510 --> 06:56.860 that with a class like so. 06:56.950 --> 07:07.780 First of all we need to imported from risk framework dot routers import default a router then yeah we 07:07.780 --> 07:14.080 can just delete these two and boys which are going to be generated automatically and the same fee for 07:14.080 --> 07:15.660 the two views here. 07:15.880 --> 07:18.070 We need to initialize the router instead. 07:18.100 --> 07:25.710 So a router equals default router and we can now use this router instance to register get point that 07:25.720 --> 07:26.510 we want to use. 07:26.560 --> 07:35.570 So a router that register and so we want to register the profiles and point was going to be a rostering. 07:35.580 --> 07:38.880 So I'm going to place an R in front of it and end here. 07:38.950 --> 07:41.230 I'm going to pass a profile. 07:41.230 --> 07:42.810 Use it like so. 07:42.830 --> 07:48.130 So the router will automatically generate the links the different end points for us and therefore we 07:48.130 --> 07:51.390 can include them within that EU URL path at least. 07:51.430 --> 07:55.470 Like if they were part of a completely different app so path. 07:56.830 --> 07:57.870 Include. 07:58.140 --> 08:02.540 Which you also need to import of course. 08:02.620 --> 08:07.210 And so here we can pass through the thought you address. 08:07.480 --> 08:10.270 Let's now restart the development server. 08:10.470 --> 08:14.270 Let's go back to Chrome and let's check the endpoints once again. 08:14.300 --> 08:19.770 So I'm just going to guess the difference of the same instance we see that we get the details and the 08:19.770 --> 08:20.710 name is changed. 08:20.730 --> 08:24.820 Now it's profile instance and we also have a profile list. 08:25.080 --> 08:27.640 Which of course does exactly what you would expect. 08:27.660 --> 08:34.560 Not only that we now also have API road and we can read it provides us the default basic route view 08:34.680 --> 08:40.780 for default router and we got a link to the main endpoint profile list. 08:40.860 --> 08:46.380 So going back to visit your code once again there's no CEO to make a profile of view said even more 08:46.380 --> 08:52.050 powerful in order for us to be able to also update this single instances all from the same as you said 08:52.050 --> 08:52.900 glass. 08:52.910 --> 08:55.390 And so in order to do so we're going to need two things. 08:55.410 --> 09:00.930 First of all the genetic view sets glass and then all the mixing is related to the behavior that we 09:00.930 --> 09:02.290 want to provide. 09:02.430 --> 09:06.950 We no longer need read only more if you said I can just commented out. 09:07.040 --> 09:12.120 We're going to need few settings that so for on the rest framework. 09:12.510 --> 09:15.650 Import view sets and as we said sense. 09:15.690 --> 09:21.540 So from rest framework import mixes and now here. 09:21.600 --> 09:23.750 First of all make sense. 09:23.750 --> 09:25.480 Don't update model. 09:26.360 --> 09:32.200 Maxine then exceeds that list model Maxine. 09:32.500 --> 09:36.330 Maxine is not retrieve more that Maxine. 09:36.330 --> 09:40.990 And of course you said that generic view set. 09:41.220 --> 09:46.680 We know that thanks to prophecy that Isaac except for the Avatar field we can update all the other fields 09:46.710 --> 09:51.870 in a profile instance because of that we want to be sure of course that to instances can be updated 09:51.930 --> 09:53.180 only by their own. 09:53.190 --> 09:59.760 So basically we need to define a specific permission class let's create a new file within the EPA folder 10:00.890 --> 10:10.890 admissions that by and of course we need to import permissions so from rest framework the import permissions 10:11.420 --> 10:20.840 and we could create our class so class up his own profile or read only which clearly extends admissions 10:20.850 --> 10:24.740 that base permission and then yeah. 10:24.780 --> 10:31.240 Def as object permission but we do not need this code instead. 10:31.500 --> 10:36.450 First of all we want to check if their request method is in permissions dot safe methods. 10:36.480 --> 10:47.700 So if a request that not method in permissions that safe methods ended case we return True right away. 10:47.700 --> 10:54.030 Otherwise the return object not user equals request that user. 10:54.260 --> 11:00.090 And that's because as you probably remember in the profile model we've defined user as Model S not one 11:00.090 --> 11:02.010 to one field to user. 11:02.010 --> 11:07.530 Which of course we're using to authenticate our users therefore request not user can be compared to 11:07.530 --> 11:11.390 the user instance we can now import the class right away. 11:11.390 --> 11:13.620 We've seen that use that by file. 11:13.770 --> 11:22.590 So from profile stored API dot permissions import is on profile ordinary don't leave we can just as 11:22.590 --> 11:31.440 well switch position like so and we can now add the new class to the permission classes list. 11:31.440 --> 11:32.520 So let's test it. 11:32.940 --> 11:38.060 Let's restart the server once again and let's go back to cruel. 11:38.940 --> 11:48.590 So I'm currently logged in as admin try to update the profile with Ida 3 instead so so as you can see 11:48.590 --> 11:55.550 of course the end point accepts put and patch up but no update forum is shown because this is not our 11:55.550 --> 11:56.160 profile. 11:56.330 --> 11:59.820 Let's try to update our profile instead. 12:00.010 --> 12:00.540 One. 12:00.770 --> 12:05.120 And as you can see now of course we can update both biography and city. 12:05.120 --> 12:13.370 For example I could write something like REST API administrator put and edit is perfect. 12:13.370 --> 12:19.190 So it's finally time to write some code to manage the second point in our profile sop up the profile 12:19.200 --> 12:22.430 status model and to do so. 12:22.440 --> 12:29.810 We're going to use another if you set that model view set which is so powerful that provides us full 12:29.810 --> 12:33.230 crowd functionality without having to use the mixing classes. 12:33.230 --> 12:37.690 We also need to import both the model profit status and dissident eyes. 12:37.850 --> 12:49.340 So profile start was nicer and we kind of defined our new of class so class up profile start to view 12:49.350 --> 12:57.630 set which is going to extend the view set and we're going to need all these attributes like so. 12:57.690 --> 13:03.480 But this time of course it's going to be provide start with the objects at all in the asset class is 13:03.480 --> 13:05.480 going to provide start to see analyzer. 13:05.490 --> 13:14.340 We also need to create a new mission class so so because of course using profile start to view set which 13:14.340 --> 13:20.460 extends model view said we are going to be able to read update and delete the single profile start to 13:20.460 --> 13:21.200 see instances. 13:21.210 --> 13:27.840 Therefore the most powerful capabilities update and delete must be granted exclusively to the instance 13:27.900 --> 13:28.890 owners. 13:28.890 --> 13:30.530 So let's go back to permissions. 13:30.570 --> 13:40.470 And here we can just call it is owner or if it only in the area we can refer object to user profile 13:40.680 --> 13:45.420 equals request dot user not profile. 13:45.420 --> 13:47.670 Actually pretty simple. 13:47.670 --> 13:57.020 So let's go back to use that by file let's import permissions and let's change the permission classes 13:57.020 --> 14:03.320 attribute like so now considering that we want to automatically connect the new profile start to see 14:03.320 --> 14:08.960 instance to the profile of the user that is making the request we now have to override the perform create 14:08.960 --> 14:09.420 method. 14:09.440 --> 14:17.170 So DFA therefore create which is always accepts self and see nicer. 14:17.210 --> 14:27.760 And so here are user profile equals self not requested the accuser both profile and then utilize her. 14:28.850 --> 14:33.850 Let's say we have user profile equals user profile. 14:34.070 --> 14:37.260 And of course this happens because the model of you said. 14:37.580 --> 14:41.000 As you can see is composed by all the different mixes. 14:41.360 --> 14:46.710 So let's now go back to the has stood by file because of course we need to register the new view set. 14:46.730 --> 15:01.280 So profile status you set and so here you can register Stathis they need to provide that as you said. 15:01.610 --> 15:04.590 So let's restart our development server once again. 15:04.850 --> 15:11.410 Let's go back to Chrome you can actually go to the EPA route and you see that now we got. 15:11.800 --> 15:18.710 So let's create our first stop was that was content hell a world post and you see user profile ad means 15:18.720 --> 15:22.950 that there's content creator dead and updated that we can go back to profiles. 15:22.960 --> 15:25.030 That was Lisa we clearly only have one. 15:25.030 --> 15:36.670 So let's create another one second status post if we wanted to we could now update em like so for example 15:36.790 --> 15:43.300 adding some more exclamation marks then put two and it it is updated and you see that we also have the 15:43.300 --> 15:44.110 delete button. 15:44.320 --> 15:51.070 But let's now test have another user so log out and let's log making as rest. 15:51.130 --> 15:51.520 Test 15:54.760 --> 15:55.590 like so. 15:55.690 --> 16:01.510 And of course you see we now see the details of this specific statues but we can't neither updated or 16:01.600 --> 16:02.930 deleted. 16:03.220 --> 16:07.870 So as you can see Vue said routers are really really powerful. 16:07.900 --> 16:13.480 And even though they might be working on a very high abstraction level always remember to check their 16:13.480 --> 16:19.040 source code and generous frameworks documentation if you need specific details. 16:19.060 --> 16:25.480 There are however some projects where using both view said glasses and concrete the view glasses is 16:25.480 --> 16:27.730 the best design choice possible. 16:27.730 --> 16:34.270 For example the specifications for our project required us to create an endpoint to let our users update 16:34.270 --> 16:34.970 data about that. 16:35.080 --> 16:37.720 That's why we've created this specific serialized it. 16:37.870 --> 16:44.210 So in this case it's much better to use a genetic update EPA view class to build the endpoint. 16:44.650 --> 16:46.320 So you're class. 16:46.580 --> 16:57.960 How about that update view which is going to extend genetics not update EPA view we need of course to 16:57.960 --> 17:00.710 import. 17:01.180 --> 17:02.650 So profile about that. 17:03.080 --> 17:03.910 Utilize it. 17:04.580 --> 17:05.340 Yeah. 17:05.420 --> 17:16.420 We can just so two and three we feel it to pull also like so. 17:16.560 --> 17:26.440 So here we can pass serialize that class and remedial classes like so and is indicated is enough. 17:26.630 --> 17:30.680 We do not need to define what he said because we are now going to other right. 17:30.710 --> 17:40.660 D Get object mattered so that we can have an endpoint in the form of API is Lesh Avatar where there 17:40.660 --> 17:46.840 is no need to pass any primary key because we are going to use the get object method to automatically 17:46.840 --> 17:54.010 identify and return to the profile instance associated with request don't use it so that that specific 17:54.010 --> 18:00.870 profile instance can be used by profile etc. either to update the avatar field. 18:01.090 --> 18:02.610 So it's pretty easy. 18:02.830 --> 18:14.650 We can define profile object as self though to request the user profile and then we can just return 18:15.250 --> 18:16.270 profile object. 18:16.270 --> 18:21.000 So let's not define the API is less about the endpoint in the understood by phase. 18:21.000 --> 18:29.190 So first of all we need to import the view and I can use a tuple here just as well. 18:32.260 --> 18:42.750 So or maybe just want to and then here we can just define it as any other end point. 18:42.760 --> 18:50.990 So how about that who's going to call out that update view dot s view. 18:51.040 --> 18:54.960 Name equals habitat update. 18:55.060 --> 18:59.650 Going back to the views that I find that is one last feeling that we need to update that to realize 18:59.650 --> 19:07.520 that a glacier like so so let's restart our development server and let's go back to Chrome. 19:07.520 --> 19:09.040 First of all we need to look at 19:12.920 --> 19:14.220 will look Dean as the rest. 19:14.230 --> 19:14.760 Test. 19:14.770 --> 19:19.100 Let's go to API slash Avatar. 19:19.120 --> 19:22.300 We can select an avatar for our arrest test profile. 19:22.810 --> 19:27.000 And yeah I can just select the python logo so put it. 19:27.220 --> 19:29.890 Here is our avatar. 19:30.370 --> 19:37.090 Let's go to the profiles list so API root profiles and you'll see that the admin user still as a red 19:37.420 --> 19:39.430 square as an avatar. 19:39.430 --> 19:41.490 Random still less no Avatar. 19:41.830 --> 19:44.130 And of course again rest the test. 19:44.180 --> 19:45.700 A python logo. 19:45.820 --> 19:50.760 Okay so that was it for this lecture about the view said Andrew router classes in the next lecture. 19:50.770 --> 19:53.320 We're going to talk about filtering in January's framework.