WEBVTT 00:00.120 --> 00:05.910 Hello everyone and welcome to this new lesson one of the key benefits of using the genetic views in 00:05.910 --> 00:12.480 Django is in Django framework is that this is offered us a large amount of ready to use code that is 00:12.480 --> 00:18.990 useful in most of the most common development scenarios CRUD operations in a model backed API for example 00:19.290 --> 00:25.470 will be implemented in the same way in most cases and because of that reinventing the wheel every type 00:25.770 --> 00:27.720 would be a waste of time. 00:27.750 --> 00:34.140 Django this framework is a class called generic KPI a view that extends the EPA view class already seen 00:34.140 --> 00:35.560 in previews lessons. 00:35.640 --> 00:41.540 I think these are some very useful methods and attributes the generic API classes often use to weave 00:41.550 --> 00:48.780 mixes which are classes that provide further functionalities to our views increasing their capabilities. 00:48.900 --> 00:55.410 It is important to note and understand that as we will see the mixing classes provide action methods 00:55.680 --> 01:02.670 such as leased or create rather than defining the handler methods such as GET or POST directly as we 01:02.670 --> 01:06.620 did using the EPA view class before starting to write code. 01:06.660 --> 01:08.900 Let's jump to the documentation first. 01:09.060 --> 01:14.760 So here we are in January as frameworks documentation you can access the page related to the generic 01:14.760 --> 01:21.030 API view class from the drop down menu API guide and then clicking on generic of use and then right 01:21.030 --> 01:28.410 here on the left generic API view is we've briefly mentioned the generic API view class extends Django 01:28.410 --> 01:34.470 test frameworks API view class adding commonly required behavior for standard at least in detail views 01:34.830 --> 01:40.760 to control the behavior of the classes that we are going to write using the genetic API view class we 01:40.770 --> 01:47.260 need to set some attributes which are going to be related to the rest of the code that we write in and 01:47.280 --> 01:51.960 as you can see among these attributes we can find query set which is going to be used for returning 01:51.960 --> 01:57.690 object from this view that we have serialize their class which clearly we are going to have to assign 01:57.690 --> 02:03.720 to this that a class that we want to use for C rise in a day serialized in data and as we've already 02:03.720 --> 02:10.680 said we often want to combine two day genetic EPA view class mixing classes which are going to be in 02:10.680 --> 02:17.520 fact the classes that we provide the actions that are going tend to be used to provide the basic behavior 02:17.880 --> 02:18.890 to our class. 02:18.990 --> 02:25.980 For example you see we got list model Maxine that implements least in inquiry set then we got to create 02:25.980 --> 02:32.520 model Maxine that implements creating and saving a new model instance and so on and so forth. 02:32.610 --> 02:35.020 So it's definitely time to make some examples. 02:35.040 --> 02:37.600 Let's jump to these to your code. 02:37.610 --> 02:37.980 Okay. 02:38.000 --> 02:43.310 If we are invisible to the code and the first thing that we need to do is to create a views that by 02:43.310 --> 02:53.070 file within the API folder here we need to make two fundamental imports for this lesson from the rest 02:53.070 --> 02:54.570 framework. 02:54.630 --> 03:04.140 Import generics and from risk framework import mixes let's also import more data since they realize 03:04.140 --> 03:04.410 there. 03:04.440 --> 03:21.690 So from e-books that models import e-book and from ebooks that API the supervisors import e-book analyzer. 03:21.690 --> 03:34.950 So let's write our view class e-book least create API view and first of all generics dot generic API 03:34.950 --> 03:35.610 view. 03:35.610 --> 03:42.120 We've also said that in order to actually provide some behavior functionalities to our classes we need 03:42.120 --> 03:48.700 to mix the mixes with the generic API class in the context of these EPA view. 03:48.840 --> 03:53.870 We can therefore use the least modal mixing and create model mixing. 03:53.970 --> 03:59.920 So we need to actually use them before the generic API view class. 04:00.240 --> 04:12.510 So Maxis does at least model Maxine and mixes not create model mixing before implementing the rest of 04:12.510 --> 04:13.110 the code. 04:13.150 --> 04:15.690 Let's ever look at a classic definition. 04:15.700 --> 04:23.610 So this is the generic API view class which as you can see extends the EPA view class you see that here 04:23.670 --> 04:30.030 we need to set these specific attributes or override some methods which we're going to do later on in 04:30.030 --> 04:31.410 the next lessons. 04:31.410 --> 04:36.750 But for now you see you can define for example query set and serialize her class which are basically 04:36.750 --> 04:38.550 two of the most important ones. 04:38.550 --> 04:43.620 And then you see there are some other very useful attributes for example lookup field which allows you 04:43.620 --> 04:50.460 to specify a different field instead of the canonical primary key to perform object lookups and then 04:50.460 --> 04:53.320 of course we can find implemented some other methods. 04:53.640 --> 04:56.880 Let's now have a look at the code of the mixes. 04:56.880 --> 05:04.580 For example the least modern Maxine go to definition and you see it provides the least method. 05:04.680 --> 05:11.150 So in action method which will then provide the response based on that kind of action performed. 05:11.160 --> 05:18.800 Okay cool let's see how everything fits together within our own class so here I'm going to define query 05:18.800 --> 05:30.780 set which in this specific case is going to be a book that objects not all and then serialized class 05:31.260 --> 05:34.930 equals ebooks utilize it. 05:35.110 --> 05:41.050 We now need to define the handler methods in this case get and post which will then return the behavior 05:41.080 --> 05:43.330 provided by the action methods. 05:43.660 --> 05:57.820 So def get a screen to accept self request documents and keyword arguments and it's going to return 05:58.460 --> 06:00.530 self-taught at least. 06:01.000 --> 06:03.240 And yet going to pass the request. 06:04.060 --> 06:08.270 And then how did you humans keyword arguments. 06:08.400 --> 06:13.600 And in case you're new to this kind of definition arguments and Q What about arguments. 06:13.740 --> 06:19.920 This is just a convention that we use to actually tell our functions that they might need to manage 06:20.010 --> 06:25.950 a variable number of arguments and keyword arguments without needing to know exactly what the other 06:25.950 --> 06:28.050 Mandarin for example is going to require. 06:29.170 --> 06:36.950 Now we've got post def post which is going to accept the same parameters I can in fact copy everything 06:36.950 --> 06:37.940 like so. 06:37.940 --> 06:44.660 But this time I'm going to call self that create we now need to connect of you to where you are real 06:44.810 --> 06:52.310 and points from when to create you have to start by five weaving the API folder in here from Django 06:52.310 --> 07:09.730 that you rails import bath and from ebooks dot API dot views import e-book at least create API view 07:11.060 --> 07:22.870 now your real partners it's going to be at least it's always path books which is going to call e-book 07:22.940 --> 07:32.120 least create API view dot s view and as a name we can pass a book 07:34.920 --> 07:41.540 as always we now need to connect the three year old stood by files so I'm going to import include. 07:41.700 --> 07:57.910 I'm going to use it right below your SO path API include ebooks API noted that we can now run our development 07:57.910 --> 08:02.810 server which we actually need to configure so at configuration. 08:02.900 --> 08:04.480 Python. 08:04.710 --> 08:08.140 I'm going to comment out as always no threading. 08:08.360 --> 08:10.210 And now we can launch it 08:15.620 --> 08:16.700 everything works fine. 08:16.700 --> 08:22.590 Let's go to Chrome so let's go to slash API slash eBooks. 08:23.540 --> 08:27.710 And clearly at the moment we get an empty list even though the status quo is to undo it. 08:27.740 --> 08:28.430 OK. 08:28.550 --> 08:31.940 And that's because we haven't added yet any instance. 08:31.940 --> 08:39.320 But as you can see we know where the very useful form that we can use to actually define the Jason that 08:39.320 --> 08:40.780 is going to be sent. 08:40.940 --> 08:46.910 This is another very useful feature of the browser API when used in combination with a view that's a 08:46.910 --> 08:48.110 little bit sophisticated. 08:48.110 --> 08:53.780 Let's say like the one we created we have the generic API of your class so let's create an instance 08:53.960 --> 09:12.270 for example zero to 1 author Peter Thiel description notes on start ups or how to build the future. 09:12.680 --> 09:20.950 And he was published on this date 0 9 16th two thousand fourteen. 09:21.040 --> 09:24.650 Let's post and it is a beautiful instance. 09:24.680 --> 09:25.940 Perfect. 09:26.060 --> 09:27.550 Let's add another one. 09:27.550 --> 09:30.340 So I'm going back to the first point. 09:30.680 --> 09:36.140 Maybe computer networks by Andrew 09:39.710 --> 09:51.140 Tannenbaum description our full introduction to computer networks and to set the publication date. 09:51.140 --> 09:57.990 We can also take advantage of a very powerful widget you see that is provided by the browser API. 09:58.190 --> 10:02.420 Even though I actually don't remember the publication date of this book so I'm just going to write 0 10:02.420 --> 10:13.100 1 0 1 2010 something like that post and if we now go back to the midpoint and we got a list of ebooks. 10:13.230 --> 10:13.510 Okay. 10:13.540 --> 10:19.960 So that was it for this lesson about genetic EPA view class and maxis and in the next lecture we're 10:19.960 --> 10:24.220 going to start talking about their so-called concrete view classes. 10:24.430 --> 10:30.370 Basically class based the EPA views that are based on top of the genetic EPA view class and several 10:30.370 --> 10:30.940 mixes.