WEBVTT 00:00.180 --> 00:02.750 Hello everyone and welcome to this new lesson. 00:02.840 --> 00:08.430 Now that you've learned how to use the generic EPA view and mixing classes we can finally talk about 00:08.430 --> 00:11.340 the generic classes that you're going to be using. 00:11.340 --> 00:18.120 Most of the time because of their practicality and power in generous frameworks docs are called concrete 00:18.210 --> 00:25.980 few classes because these are in fact the real concrete genetic views that implement most of the functionalities 00:26.370 --> 00:28.230 that a developer might need. 00:28.230 --> 00:29.830 How do they work. 00:30.060 --> 00:36.870 Each one of their concrete view classes extends their generic EPA view class and those mixes that offer 00:36.870 --> 00:39.720 the functionality that the class is meant to provide. 00:39.720 --> 00:47.330 The class retrieve update EPA view for example will extend the generic EPA view class plus both retrieve 00:47.330 --> 00:52.590 more than scene and update more Maxine because of their obstruction level. 00:52.650 --> 00:59.580 They are surely the fastest to right and easiest to read but at a cost of being also the most magical. 00:59.580 --> 01:05.550 It is therefore very important to keep in mind what we have seen so far in order to use them comfortably 01:05.820 --> 01:08.880 knowing exactly if when and how to customize them. 01:09.060 --> 01:09.960 Let's get started. 01:10.830 --> 01:15.790 If we are invisible to your code and considering that in this lesson we're going to see how powerful 01:15.790 --> 01:18.550 that concrete view classes really are. 01:18.550 --> 01:23.890 I'm not going to comment this class we're actually going to write it. 01:23.980 --> 01:28.010 Once again this time using a concrete view class. 01:28.030 --> 01:37.510 So I'm going to copy the name class e-book least create EPA view which is going to extend generics dot 01:37.870 --> 01:41.620 least create API view. 01:41.620 --> 01:48.370 We can now simply set up the two attributes query set and see that I set a class. 01:48.640 --> 01:49.200 This way. 01:49.270 --> 01:51.280 We've only 3 lines of code. 01:51.280 --> 01:56.460 We've created a perfectly functioning class that is going to do the same as the one that we've written 01:56.470 --> 01:57.900 the previous lesson. 01:58.000 --> 02:03.140 See if you go and have a look at the definition of list create API view. 02:03.310 --> 02:07.890 We can see that it is basically the same class that we've written ourselves. 02:08.020 --> 02:11.500 Now that doesn't mean that the view we've written is useless. 02:11.500 --> 02:18.620 On the contrary it means that using the concrete view classes we're working at are yet abstraction level. 02:18.610 --> 02:21.000 Let's not define that detail view. 02:21.100 --> 02:33.440 So classic e-book detail API view which is going to extend generics dot retrieve update destroy API 02:33.460 --> 02:34.250 view. 02:34.420 --> 02:40.340 We can pass the same values for the two fundamental let's say attributes. 02:40.540 --> 02:46.630 Then if you go and have a look at retrieve update destroy VBA view you can see that it's a class that 02:46.630 --> 02:55.630 is using generic EPA view plus three different mixes so time to actually create some endpoints for our 02:55.630 --> 02:56.260 views. 02:56.680 --> 02:58.420 So let's go to that. 02:58.960 --> 03:09.600 You are by file and I'm going to import the class and I'm going to create an end point so X1 ebooks 03:10.230 --> 03:14.420 in the area can pass integer primary key. 03:14.520 --> 03:20.040 The view is going to be e-book detail API view and any e-book detail. 03:20.580 --> 03:28.670 You might remember from the previous lesson that integer decay is the default lookup field. 03:28.920 --> 03:38.400 If you're gonna have a look remember that generic API view you see that you're in lookup field. 03:38.480 --> 03:41.180 We got P.K. as a default. 03:41.180 --> 03:47.350 So if we needed let's say to use as lag we might want to change lookup field and look up your role. 03:47.360 --> 03:49.880 Q What are the argument accordingly. 03:49.880 --> 04:00.240 So let's now run a development server and let's test our newly created in points us open up chrome India. 04:00.540 --> 04:07.460 We can go to the Lesh API and here we see her 20 points. 04:07.470 --> 04:15.390 So as Lesh e-books and data we get at least we've three books plus the HDMI all four that remember is 04:15.390 --> 04:20.740 provided because we are actually using a class that extends generic API view. 04:20.790 --> 04:31.560 Them check the details of a single book like for example 0 to 1 and you see we get the HDMI 4 and the 04:31.660 --> 04:38.580 G sounded are already pre compiled and we also get a button to delete things this as a matter of fact 04:38.610 --> 04:43.190 we can see all the h to be verbs that are allowed in D and point. 04:43.380 --> 04:49.830 So let's say for example we needed to update the description for this specific instance. 04:50.070 --> 04:58.590 We can use the practical HDMI all way and maybe you can just add something like updated pewter and you 04:58.590 --> 05:02.100 see we made it put the request towards the endpoint. 05:02.250 --> 05:03.480 We get 200. 05:03.520 --> 05:07.370 Okay and the updated Jason of the instance. 05:07.770 --> 05:14.850 Let's now go back to his words to the code and let us know write the review create API view class. 05:14.850 --> 05:24.530 So first of all we're going to need to import our review analyzer and our review model. 05:24.830 --> 05:37.340 Then right here below we can go ahead and create a review class review create API view which is going 05:37.340 --> 05:42.050 to extend the generics dot create API view. 05:42.050 --> 05:55.300 And here we can define query set as review dot object dot all and see realize the class as review serialize 05:55.670 --> 05:56.040 it. 05:57.280 --> 06:04.490 We have previously mentioned dead the concrete view classes are in fact among the fastest to right and 06:04.550 --> 06:06.200 easiest to read. 06:06.200 --> 06:12.350 But because of the abstraction level they're also among the most magical which means that most of the 06:12.350 --> 06:16.660 software machinery that actually generates the behavior will be used. 06:16.730 --> 06:22.550 It's a from us and therefore we need to know how to personalize them to fit our needs. 06:22.730 --> 06:29.360 And in this case we want to write a class to create new review objects that we also want to automatically 06:29.360 --> 06:31.780 link to a specific book. 06:31.820 --> 06:41.060 Also because if we take a look at the review model we see that the relationship is expressed therefore 06:41.090 --> 06:50.240 clearly we actually need to connect any book instance so we can do so by overriding deeper form create 06:50.240 --> 06:58.370 method so def perform create which is going to accept Self and IT serialize her. 06:59.810 --> 07:02.270 So what do we actually want to do with this method. 07:02.480 --> 07:09.050 Well first of all we need to decide are we actually going to know which book we are going to review. 07:09.050 --> 07:10.440 And that's pretty easy. 07:10.460 --> 07:17.120 We can define a keyword argument in the real end point and then use that keyword argument to perform 07:17.330 --> 07:18.860 the object look up. 07:18.950 --> 07:20.320 So because of that. 07:20.480 --> 07:25.700 And to keep the code in order I can actually delete this one. 07:26.000 --> 07:27.870 We have to make an import. 07:27.890 --> 07:35.630 So from risk framework dot generics import get object or for or 4 and we can now actually write the 07:35.630 --> 07:38.030 code for our pure form create method. 07:38.180 --> 07:50.390 So e-book became equals self doubt keyword arguments that get we want to get e-book P.K. which is going 07:50.390 --> 07:56.210 to be the name of the keyword argument that we're going to define in the URL path based on this primary 07:56.210 --> 07:56.630 key. 07:56.630 --> 08:05.060 We can actually get these bookings as we want to review so e-book equals get objects for a form of e-book 08:05.170 --> 08:10.330 with primary key equals e-book primary key. 08:10.500 --> 08:17.780 And now we can actually call serialize or save into the save method. 08:17.850 --> 08:26.280 We can pass e-book equals e-book so let's have a look at the code of create API view. 08:26.280 --> 08:29.690 We see that clearly it everything from create model magazine. 08:30.060 --> 08:38.950 So let's have a look at the code of that specific magazine from rest framework import axes. 08:39.060 --> 08:46.740 Let's have a look and you see this is the create method which at the point calls self taught per perform 08:46.740 --> 08:47.760 create. 08:47.760 --> 08:53.010 Basically this method which just calls the safe method of the serialize her. 08:53.010 --> 09:03.620 So in passing the e-book instance right here we are basically linking this specific instance of e-book. 09:03.700 --> 09:06.540 We've got a newly created review object. 09:06.780 --> 09:13.470 We now need to alter the serialized or the review sterilizer so that we can exclude from the sterilization 09:13.500 --> 09:15.600 and the sterilization process. 09:15.600 --> 09:21.980 The e-book so exclude equals a book. 09:22.080 --> 09:29.060 Now let's go ahead and before we test the newly created class let's create another one. 09:29.320 --> 09:38.430 Then what we're going to use for the detail and point so class review detail API view which is going 09:38.430 --> 09:47.970 to extend generics dot retrieve update destroy EPA view and we can define the same attributes we've 09:47.970 --> 09:49.220 defined up there. 09:49.260 --> 09:50.940 So perfect. 09:51.500 --> 09:53.960 Let's now define the new endpoints. 09:53.960 --> 09:59.560 So first of all we need to import diffused light. 09:59.570 --> 10:03.270 So one and 10:07.800 --> 10:09.510 we can create the two new endpoints. 10:09.510 --> 10:19.650 So first of all going to copy these and find like so we're just going to call review create VBA view 10:19.680 --> 10:28.420 dot as view and its name is going to be a book review and the path is going to be ebooks. 10:28.590 --> 10:30.960 Integer e-book. 10:31.030 --> 10:31.650 Okay. 10:31.680 --> 10:37.840 And then review we can change the indentation a little bit. 10:37.910 --> 10:40.090 One two. 10:40.140 --> 10:47.270 And then the second point and ear. 10:47.870 --> 10:51.740 And we can now define the last end point. 10:51.770 --> 11:01.900 So thanks so this is going to call review detail EPA view and dot as view is going to be called Review 11:04.120 --> 11:13.090 detail and the path is going to be reviews slash integer primary key so time to test our new endpoints. 11:13.150 --> 11:24.710 Let's run the development server that's now go to Chrome it is our e-book least up so let's try to review 11:25.400 --> 11:26.550 0 to 1. 11:26.570 --> 11:33.170 I'm going to API ebooks one review for words last year. 11:33.190 --> 11:37.550 We're making a get request so we get a GDP for 0 5. 11:37.550 --> 11:44.900 Method not allowed but nevertheless because we are using the developer browser API we get these HCM 11:44.900 --> 11:48.710 CML form that we can use to actually create a new review. 11:48.710 --> 11:53.940 Considering that we're excluding the book field we only get to review our total review and rating. 11:53.990 --> 11:58.070 So I'm going to create this review preview author Michela review. 11:58.190 --> 12:00.710 Great book. 12:00.940 --> 12:10.670 Rate in 5 post and edit is we've just successfully reviewed the book and if we now go to the e-book 12:10.670 --> 12:15.380 least and point we get HP 200 everything OK. 12:15.400 --> 12:22.730 And you see now that 0 2 1 also shows a list with the reviews available. 12:22.730 --> 12:26.180 Let's say we now wanted to update this specific review. 12:26.660 --> 12:35.600 We might just go to API reviews 1 for what is Lesh and you'll see we get the details of this specific 12:35.600 --> 12:44.090 review and both the h the email 4 and D J soon are actually pre defined and we can now just change them. 12:44.090 --> 12:48.100 So let's say I want to update the review update. 12:48.890 --> 12:53.710 Even better than I thought. 12:53.840 --> 12:59.950 So let's click on puter and edit is just updated our review. 13:00.110 --> 13:00.350 Okay. 13:00.380 --> 13:06.050 So that was it for this lecture dedicated to the concrete view classes in Django this framework as you 13:06.050 --> 13:12.650 can see they are really really powerful and pretty easy to implement even though we must take into consideration 13:12.890 --> 13:18.860 that sometime we might want to check the source code of the classes to actually unveil a bit of their 13:18.860 --> 13:22.160 magic allowing you to customize them when needed. 13:22.160 --> 13:23.660 See you in the next lecture.