WEBVTT 00:02.530 --> 00:08.400 In this lesson we're going to create this utilize classes for that question and answer with models. 00:08.410 --> 00:15.470 So the first thing that we need to do is to create an API folder and inside of this four that we can 00:15.470 --> 00:18.270 now create the Syrian eyes. 00:18.270 --> 00:22.300 It's not by fight let's import everything that we need. 00:22.300 --> 00:35.440 So from rest framework the import utilizes and from questions that models import passwords and question. 00:35.470 --> 00:38.430 First of all let's create as it serialize that. 00:38.460 --> 00:41.410 So a class answer. 00:41.760 --> 00:47.620 It's even nicer synchronize its dot model. 00:47.770 --> 00:48.430 You realize that. 00:48.990 --> 00:53.980 And even though the more they'll realize that a class provides us the standardized that fields based 00:54.040 --> 00:59.440 on the model for out of a specific project we still need to add a couple of fields. 00:59.440 --> 01:04.000 So first of all we want the string representation of the offer field. 01:04.060 --> 01:11.050 Therefore let's define author as that ISIS dot string related field. 01:11.050 --> 01:19.990 We've read only equals true and we're not going to define a couple of serialize that metal fields. 01:20.110 --> 01:24.590 First of all a method field for created. 01:24.760 --> 01:29.950 So that we can customize the order of day month and year in our data fields. 01:29.950 --> 01:39.460 So she realizes dot serialize the method field we've read only equals through we're going to ignore 01:39.460 --> 01:44.100 the updated add field that anyways is going to be available if you need to. 01:44.110 --> 01:51.430 You can just customize the sterilizer and instead we want to define a likes count field which as the 01:51.430 --> 01:58.900 name clearly suggests will give us the number of likes for a specific answer and then we're going to 01:58.900 --> 02:07.060 ever use that as voted a field which is going to tell us if the request user has already left the like 02:07.150 --> 02:09.050 on the answer where we're looking at. 02:09.220 --> 02:15.730 And this of course is going to be extremely helpful when creating a user interface for our application. 02:15.730 --> 02:21.790 Let's know defining class met up with model equals elsewhere of course. 02:21.800 --> 02:29.820 Antonia we want to exclude three fields the question voters and updated at fields. 02:29.920 --> 02:42.160 So here we can simply use the exclude attributes to define a list that we've question motives and updated 02:42.610 --> 02:43.100 it. 02:43.480 --> 02:45.730 Let's not define all the methods we need. 02:45.730 --> 02:55.320 So first of all def get creative debt to self and investors. 02:55.560 --> 03:05.460 And yet we can return things that's not created to dot s t r f time and yet we can't customize the way 03:05.460 --> 03:07.620 we want our dates to look like. 03:07.620 --> 03:11.990 I'm just going to use the American International way let's say to display dates. 03:12.000 --> 03:17.510 So first of all the month then today is the digit. 03:17.610 --> 03:19.940 And then the year. 03:20.100 --> 03:21.910 So the date is going to be shown as. 03:21.910 --> 03:24.390 Name of the month the date. 03:24.460 --> 03:27.150 We have a digit and then dear. 03:27.300 --> 03:31.520 So for example December 20 the 5th 2019. 03:31.720 --> 03:33.790 Let's then define the other methods. 03:33.900 --> 03:36.810 So get likes count 03:39.470 --> 03:44.650 which is going to return instance that voters don't count. 03:45.320 --> 03:48.620 And then you had a below we can define the last method. 03:48.620 --> 04:03.500 So get user as voted an idea first of all we need to define request as self not context dot gets a request 04:04.280 --> 04:06.100 and then you know we are going to return. 04:06.110 --> 04:06.930 Is daughter. 04:06.930 --> 04:16.220 Voters don't feel that many key equals request the user dot primary key. 04:16.820 --> 04:25.010 And then here we can just return a dot exists and as we see later on we are going to have to pass the 04:25.010 --> 04:28.110 request via context in a couple of views. 04:28.370 --> 04:32.120 But for now we can just define the method. 04:32.120 --> 04:41.700 Now let's define the question serialize it so class question either which extends serialize it's not 04:41.740 --> 04:48.500 more that I see that of course and in the first two fields are going to be the same as the one we've 04:48.500 --> 04:57.990 defined up here and then we can define as log as that ISIS dot the slug field and that's because we 04:57.990 --> 05:02.350 want to pass read only equals true. 05:02.430 --> 05:07.230 And then similarly to what we've done up here we want an answer with count. 05:07.230 --> 05:15.800 So that's where US count equals C lies us dot c that i's that mattered. 05:16.150 --> 05:29.130 FIELDER we've read only equals through and then also a user as password field which is going to be of 05:29.130 --> 05:38.730 type C rise and method fields like the fields above we can now define class Meta model equals question 05:38.790 --> 05:44.400 and we can exclude updated at. 05:44.870 --> 05:49.840 And let us know define your methods so we can just copy it get created at 05:52.850 --> 05:53.800 a b c d. 05:53.810 --> 06:04.930 Like so then we're going to have def get answers count self and those and we're going to determine instance 06:05.000 --> 06:15.310 that words that count were asked which is of course the related name we've defined here. 06:15.420 --> 06:16.970 Let's go back to the C advisers. 06:18.490 --> 06:21.200 Let's now define def get 06:23.860 --> 06:31.650 user as answer self and use those and similarly to what we've done up here. 06:31.930 --> 06:40.390 First of all we need the request and then we can return instance dot elsewhere and Dot filter this time 06:40.420 --> 06:47.570 we've author equals request dot com user not exists. 06:47.660 --> 06:56.530 And so similarly to date get use it is voted method get to use it as S word will return true if request 06:56.580 --> 07:04.890 dot to use it as in fact password this specific question instance is you can see I've been passing the 07:04.890 --> 07:11.730 read only parameter to the method fields even though they had already read only fields by themself I 07:11.730 --> 07:17.580 got confused the error in the first one and then I've been using it for all the other ones but let's 07:17.580 --> 07:19.690 remove and forget about them. 07:20.160 --> 07:26.480 So now that I see realizes I have finally completed we are finally ready to start creating our first 07:26.480 --> 07:26.850 views.