WEBVTT 00:02.530 --> 00:06.850 In this lesson we're going to be the two views related to the answer model. 00:06.940 --> 00:12.790 The first view will allow us to create new and scary instances which means that in fact will allow us 00:12.790 --> 00:19.750 to answer specific questions while the second view will allow us to retrieve all the questions instances 00:19.810 --> 00:21.730 for each single question. 00:21.760 --> 00:27.980 First of all there are a couple of functions and classes that we need to import generics. 00:28.150 --> 00:37.500 Then from the test framework dot generics import get object or form for from the rest. 00:37.990 --> 00:43.860 Framework that exceptions import validation error. 00:44.090 --> 00:52.940 And then of course we need to import the deep elsewhere more then and also the answer is a plus. 00:52.960 --> 01:05.120 Now below we can define a class password create API view which is going to extend generics dot the API 01:05.120 --> 01:06.020 view. 01:06.020 --> 01:18.240 We've quietly set equals answer dot objects dot all asset class equals and sort of set of eyes and ears. 01:18.250 --> 01:25.610 If we want to be explicit on the views level as well we can just copy the permission classes attribute 01:25.940 --> 01:27.770 using is authenticated. 01:27.770 --> 01:33.000 Of course this new class is going to be really powerful for a couple of reasons. 01:33.020 --> 01:40.060 First of all it will allow us to answer a single specific question based on as like parameter past VII 01:40.060 --> 01:46.940 and bind it will allow us to bind the request dot user as an author for the newly created is this. 01:46.940 --> 01:52.760 And last but not least it will check if request that the user is already asked where the question. 01:52.760 --> 01:55.680 And in that case it will raise a validation. 01:55.820 --> 02:01.540 All of this is going to happen inside the perform create method. 02:01.610 --> 02:05.080 So yeah I can modify the code like so. 02:05.090 --> 02:13.250 First of all request user equals self that request the user then we need to get the keyword argument 02:14.050 --> 02:20.100 flagship equals self not keyword arguments and not get it like it. 02:20.630 --> 02:22.960 Let's now get the question instance. 02:23.060 --> 02:23.780 To answer. 02:23.960 --> 02:36.090 So get object or photo for the answer to it more than we've slug equals keyword arguments like to deceive 02:36.090 --> 02:36.240 me. 02:36.240 --> 02:45.750 I don't we can no pass question equals question but before calling this save mattered we can check if 02:45.750 --> 02:52.590 they use that is already asked where these specific question is this If the question dot answers dot 02:53.160 --> 02:57.860 feel that we have also equals request user. 02:57.990 --> 03:09.200 So if this exists we can then raise a validation error with the message you have already. 03:09.290 --> 03:16.900 That's where this question now that the class is complete we can create an appropriate endpoint. 03:16.980 --> 03:28.890 So your path questions slash slag up of type lag and then answer and we want to call question view dot 03:29.390 --> 03:29.890 answer. 03:29.910 --> 03:34.120 Create a gave you dot as view. 03:34.290 --> 03:44.720 Let's also add a bit of indentation XOR and then name equals create that's. 03:45.220 --> 03:47.430 It's now time to test the new endpoint. 03:47.440 --> 03:53.130 So let's run the development server and let's move to chrome. 03:53.200 --> 04:00.340 We are of course redirected to the logging page but we need to go straight to the browser API so slash 04:00.430 --> 04:03.520 API authentication credentials were not provided. 04:03.520 --> 04:14.260 Let's look in as admin we have questions and point let's grab these questions lag so coffee and paste 04:14.290 --> 04:18.490 and then what is for Slash. 04:18.960 --> 04:22.490 And yeah we see from the title answer create API. 04:22.490 --> 04:28.330 Of course we get method not allowed because this first one of course was a get request. 04:28.490 --> 04:30.170 So let's create a new answer. 04:30.380 --> 04:39.470 Let's write something like hello world post we get a small error cannot retrieve keywords log into field 04:40.170 --> 04:47.240 and that's because we are we fast the asked Would model instead of the question model to get object 04:47.250 --> 04:53.300 or form because of course we are getting a question instance and we also need to change questions to 04:54.590 --> 05:01.490 question so that we can filter the query set of answers for this specific question needs this. 05:01.490 --> 05:09.280 Let's restart the development server and let's go back to Chrome and well let's get the details of the 05:09.280 --> 05:11.400 same question we wanted to answer first. 05:12.070 --> 05:18.380 So and So you see that of course clearly now ask where count is still 2 0 right. 05:18.400 --> 05:19.780 So let's answer the question. 05:22.410 --> 05:33.220 So now maybe we can add something like first and square instance post and eateries are first asked Twitter 05:33.590 --> 05:37.920 and if we now go back and check the details of the question we've just answered. 05:38.240 --> 05:42.870 We see that as what count is now one and now the receipt. 05:42.900 --> 05:44.730 Yeah maybe we could also add a comma. 05:44.740 --> 05:46.170 So let's go back. 05:46.220 --> 05:56.910 The results of your code API Isaias so we we can add a comma like so and also here. 05:57.030 --> 06:06.390 Let's restart the development server let's go back to Chrome and let's make the same get request well. 06:06.440 --> 06:08.460 Much better. 06:08.470 --> 06:14.300 Let's go right the second elsewhere related view so that we can get all the answers for a specific question 06:14.420 --> 06:16.740 given its lag. 06:17.060 --> 06:20.800 So let's go back to visible pseudocode India. 06:20.830 --> 06:36.480 We can create class question elsewhere least API view which extends generics not least API view India. 06:36.530 --> 06:42.470 We can just copy seen it as their class and permission classes just to be explicit and we don't need 06:42.470 --> 06:50.240 to define the query set attribute because it's better to just overwrite to get where set method so self. 06:50.510 --> 06:58.570 Yeah we can get the same kind of like attribute these used in point we love zone is like a keyword argument 06:58.570 --> 07:13.770 of course we can return that's where dot objects dot feel that question slug equals Q What is that like. 07:14.120 --> 07:21.490 Well maybe we can just call it answer at least EPA view it's more concise and then yeah we can also 07:21.540 --> 07:30.460 I'd ordered it by negative created that let's not create the appropriate endpoint. 07:30.810 --> 07:40.650 So yeah well we're going to have basically the same endpoint as you're going to just copy it so comma 07:40.750 --> 07:49.100 this is going to be and Swiss it's going to call us at least EPA view it's just going to be called and 07:49.180 --> 07:58.440 square at least let's restart the development server and let us go back to Chrome once again. 07:58.540 --> 08:06.760 So now to get all the answers for this specific question we can go to the same endpoint and swaps for 08:06.760 --> 08:07.710 what is in ash. 08:08.020 --> 08:13.420 And of course we now get our first answer where each instance and if you try to answer the same question 08:13.660 --> 08:14.200 more than one. 08:14.210 --> 08:22.860 So again as you can see we get the validation error you have already answered this question. 08:22.860 --> 08:23.290 Perfect.