WEBVTT 00:00.090 --> 00:02.430 Hello everyone and welcome back. 00:02.430 --> 00:07.350 In this lesson we're going to learn how to use them more than you realize that class simulator to jingles 00:07.350 --> 00:13.650 model form classes Model C realize it allows us to speed up the creation of serialize US based on models 00:13.650 --> 00:19.490 we defined providing us all the code that is needed for the most common development use cases. 00:19.560 --> 00:21.070 So let's get started. 00:21.640 --> 00:21.930 OK. 00:21.960 --> 00:28.510 So here we are in Visual Studio code and as you can see I've opened the serialized this dot by five. 00:28.650 --> 00:34.410 In this lesson as we said we're going to talk about the Model C realized that a class that in fact is 00:34.500 --> 00:39.690 very similar to the serialize that a class you can think about the more than a C realized that a class 00:39.810 --> 00:45.770 is the name suggests as a serialized that a class that is targeted towards a creation of satellites 00:45.770 --> 00:50.610 US based on models that we've created in the models not by file. 00:50.910 --> 00:55.650 So how does the model serialize that class and pass in the development process. 00:55.650 --> 01:03.240 Well for instance it will automatically generate for us a set of fields based on the model that we want 01:03.240 --> 01:03.700 to create. 01:03.700 --> 01:10.220 The satellites therefore which means that there is no need for us to specify all the fields as we DDA 01:10.240 --> 01:13.470 with the normal let's say sterilizer classes. 01:13.710 --> 01:20.160 It will automatically generate validators for the serialized such as unique together validators and 01:20.190 --> 01:25.860 it includes seeing both the implementation of the create and update methods. 01:25.860 --> 01:30.430 So basically it allows us to get a lot of code for free. 01:30.480 --> 01:39.000 So if we go and have a look at the serialized class itself and we see that it marries from base sterilizer 01:39.240 --> 01:41.380 it has a couple of methods. 01:41.640 --> 01:46.710 But basically when you really go and have a look at the code you understand that you basically have 01:46.710 --> 01:54.600 to write most of the code in fact that you need to build this analyze it depending on your use case. 01:54.630 --> 01:58.360 Let's now have a look at the code of the model serialize that class instead. 01:58.560 --> 02:06.310 So I'm just going to search for model so you can either end it it is. 02:06.620 --> 02:12.600 So you see a model serialize that is just a regular sterilizer except that a set of default fields that 02:12.600 --> 02:14.420 are automatically populated. 02:14.420 --> 02:21.290 Same thing for a set of default validators default to create and update implementations are provided 02:21.640 --> 02:30.830 and we can if I look at the code to understand how it is the default create method a default update 02:30.830 --> 02:38.550 method and so on forget fields get field names et cetera et cetera. 02:38.620 --> 02:44.550 So let's go back to the realized there not by file and let's make our own version of the article sterilizer 02:44.560 --> 02:47.910 class using this time that model C realized that class. 02:47.980 --> 02:50.310 Let's see how we can implement it. 02:50.350 --> 02:59.530 So I'm just going to comment this out like so and then right on top of the file I'm going to define 02:59.880 --> 03:06.400 class the article serialize that which this time is going to inherit from serialize this dot model serialize 03:06.450 --> 03:06.740 it. 03:07.690 --> 03:14.950 So first thing we need to do is to define that class matter and within a matter class we're going to 03:14.950 --> 03:21.360 specify model which is going to correspond clearly to the model we are building our satellites therefore 03:21.760 --> 03:27.880 and how we need to tell Django this framework which fields of the model we want to serialize and there 03:27.880 --> 03:29.780 are three ways we can do so. 03:29.860 --> 03:34.900 First of all we can say that we want to serialize all the fields of our model. 03:34.900 --> 03:41.500 Therefore we might want to define fields equals all like so. 03:41.500 --> 03:49.160 So in this way we are basically telling Django we want all the fields of our model. 03:49.390 --> 03:52.180 We can however specify just a couple of them. 03:52.270 --> 04:00.550 So we could do something like fields that we can use it to pull and so let's say title and description 04:02.050 --> 04:08.860 and maybe the body of that article and it is where we are basically telling Django it's free mode that 04:09.190 --> 04:13.890 we want to choose a couple of fields. 04:14.290 --> 04:20.290 And then the third way is to just define which fields we want to exclude. 04:20.470 --> 04:28.990 So we can do something like exclude equals that tuple or at least then for example we might want to 04:28.990 --> 04:31.400 exclude the I.D. the primary key. 04:32.050 --> 04:36.930 So let's restart our development server and let's go ahead. 04:36.940 --> 04:39.140 Let's go back to Chrome. 04:39.160 --> 04:42.730 Let's go ahead and check our new serialized behaves. 04:42.730 --> 04:45.460 So let's go back to the list and perfect. 04:45.460 --> 04:52.120 Did you go through a list of articles but this time as you can see DHEA field is hidden from the user 04:52.930 --> 04:57.640 it's easy to grasp how powerful the modal serialized that a class a release. 04:57.640 --> 05:03.910 Just have a look at how much code we've saved and it gets even more powerful and useful when you take 05:03.910 --> 05:10.030 into consideration the fact that you can actually define some extra fields for your sterilizer. 05:10.030 --> 05:16.470 So let's say we want a field which returns the time since publication for each and every article. 05:16.480 --> 05:17.560 It's really easy. 05:18.280 --> 05:24.760 Let's define the name of a field so time since publication. 05:24.760 --> 05:32.230 And we need to initialize it as serialize US DOT serialize the method field. 05:32.230 --> 05:39.160 Now we need to define a method which by default is going to be called get and the score plus the name 05:39.310 --> 05:41.080 of the serialized automated field. 05:41.170 --> 05:51.220 So right here I'm going to define get their score time since publication gets time since publication 05:51.220 --> 05:54.240 is going to be the method which is going to return. 05:54.280 --> 06:02.200 In fact the value for our time since publication field so he's going to accept sell for an object as 06:02.200 --> 06:03.460 parameters. 06:03.460 --> 06:05.800 I'm also going to need to import two functions. 06:06.370 --> 06:21.190 So right here on the top from Date Time report days time Dan from Django not utilize dot time since 06:22.090 --> 06:24.610 import time since. 06:24.610 --> 06:26.480 So let's now implement our method. 06:26.560 --> 06:37.720 So a publication date publication date equals object which is the instance that publication date then 06:37.720 --> 06:40.600 we need to date time at a time of a request. 06:40.870 --> 06:45.880 So now equals days time now. 06:46.320 --> 06:48.080 Now we can get our time Delta. 06:48.090 --> 06:59.760 So time Delta equals time since and we're going to pass publication date and now pretty easy. 06:59.830 --> 07:07.590 We can now return time delta so that its value can be used as value for time since publication. 07:07.600 --> 07:10.580 Let's now restart the development server. 07:10.630 --> 07:13.160 Perfect. 07:13.210 --> 07:14.570 Let's go back to Chrome. 07:15.390 --> 07:17.840 Let's refresh let's make another get request. 07:18.360 --> 07:19.090 Perfect. 07:19.120 --> 07:25.530 And now we got as you can see an extra field time since publication which in fact does exactly you see 07:26.460 --> 07:27.990 what the name suggests. 07:28.020 --> 07:33.060 Let's make a quick inspection of the article serialize there from Jangles shell so we can see how it 07:33.060 --> 07:39.000 relates to what we've seen in the previous lecture when analyzing the article serialize that made with 07:39.000 --> 07:40.520 the serialized hourglass. 07:40.590 --> 07:45.780 So I'm going to stop the development server and go into spawn a new terminal. 07:45.780 --> 07:52.060 So we are here Python managed that by Shell. 07:52.130 --> 07:55.020 I'm going to expand the terminal like so. 07:55.020 --> 08:09.190 So from news that API that serialized this import article analyzers or better article sterilizer let's 08:09.190 --> 08:13.080 just make a new instance see the equals 08:17.340 --> 08:21.680 article sterilizer and let's have a look at its representation. 08:21.710 --> 08:31.370 So print wrapper of our civilization and as you can see it's basically the same as what we've seen in 08:31.370 --> 08:34.760 the previous lecture except that now we've got the extra fields 08:38.450 --> 08:40.350 and let's variety the middle classes there. 08:40.440 --> 08:50.200 Know once again perfect so that we can check the basic implementation of create and update. 08:50.300 --> 08:51.290 Let's go back to. 08:52.760 --> 08:58.400 And I'm not going to create in New article. 08:58.680 --> 09:07.670 Maybe this one like song went to David Fields and times since publication. 09:07.940 --> 09:13.340 And I'm actually going to change the publication date so let's say we want to publish it in the future. 09:13.340 --> 09:19.040 Let's say we're scheduling the publication and yet have got saved and Laramie so we can use for it the 09:19.040 --> 09:26.920 title like so some description and maybe location Mars. 09:27.440 --> 09:31.910 So let's now create our new article and edit is awesome. 09:31.910 --> 09:36.860 It says as you can see publication date 0 minutes which is something that we might expect considering 09:36.860 --> 09:39.740 that the publication date is in the future. 09:40.040 --> 09:43.180 But most importantly everything worked as expected. 09:43.190 --> 09:46.360 Let's now see the update method behaves. 09:46.610 --> 09:49.400 So I'm going to copy the Jason. 09:49.430 --> 09:51.650 Going to delete those two fields 09:54.410 --> 10:01.890 so maybe I'm going to change the author's name so maybe John Peter. 10:02.000 --> 10:03.010 Let's see what happens. 10:03.140 --> 10:08.030 Puta perfect is you can see the article author is now John teeter. 10:08.270 --> 10:14.000 Considering that the model C realized that a class is just like a very powerful allies at a class we 10:14.000 --> 10:20.270 can of course define all the same validation criteria that we've defined right here in the previous 10:20.810 --> 10:22.340 version. 10:22.340 --> 10:32.010 The exact same way so I'm going to comment the code I did a link to the documentation to make it easy 10:32.010 --> 10:33.930 for you to check it if you want to. 10:34.590 --> 10:38.430 So let's restart the development server. 10:38.480 --> 10:39.550 Perfect. 10:39.560 --> 10:48.150 Maybe we can actually change the length to maybe 30 characters here as well. 10:48.250 --> 10:53.590 Let's go back to Chrome and let's try to trigger somebody nation error. 10:53.620 --> 10:57.450 So going to create a new article 11:01.750 --> 11:02.050 so 11:05.120 --> 11:17.010 I'm going to use the same description or maybe same name as description like so post and edit is for 11:17.020 --> 11:18.720 and read by the request. 11:18.720 --> 11:26.220 Let us know try to trigger the second validation error by creating an article or rather updating the 11:26.220 --> 11:31.450 article to have a title which is not 30 characters long. 11:33.140 --> 11:33.530 So 11:36.570 --> 11:38.830 put it for android by the request. 11:38.830 --> 11:45.540 The title is to be at least 60 characters long and that's because to be honest in fact you need to always 11:45.540 --> 11:52.740 update the error message to 30 like so well let's go back here let's refresh. 11:53.450 --> 12:01.400 Perfect so school back here number 7. 12:01.540 --> 12:01.960 So 12:05.620 --> 12:05.900 okay. 12:05.940 --> 12:07.560 So that was it for this lecture. 12:07.560 --> 12:13.140 As you can see the model is the analyzer class provides us a very convenient way to create C Eliza's 12:13.350 --> 12:16.620 based on models we've defined and as a matter of fact. 12:16.650 --> 12:21.270 Because of this convenience this is the class that we're going to use throughout the rest of the course 12:21.870 --> 12:22.770 in the next lecture. 12:22.770 --> 12:27.910 We're going to talk about another very important topic which is nested in relationships which means 12:27.910 --> 12:28.650 he realizes.