WEBVTT 00:00.120 --> 00:03.700 Hello everyone and welcome to this new lecture about Syrian ISIS. 00:03.750 --> 00:10.240 Syria ISIS allow complex state that such as squarely sets and model instances to be converted to native 00:10.260 --> 00:16.820 python on data types that can then be easier rounded into useful formats like Jason. 00:16.830 --> 00:23.290 This process is known as serialization of data Syria lasers are a very important component of Django 00:23.290 --> 00:29.040 this framework that we can easily use by employing this utilize the end model utilize their classes 00:29.570 --> 00:35.250 serialize their source or provide the death serialization allowing past data to be converted back into 00:35.250 --> 00:39.480 complex types after first validating the incoming data. 00:39.480 --> 00:44.730 In this lesson we're going to learn how to use this serialize their class to sterilize and the serialized 00:44.730 --> 00:48.870 data from a radical model that we've defined in a previous video. 00:48.870 --> 00:53.030 We're also going to briefly talk about passives and renderings. 00:53.080 --> 00:54.210 Let's get started. 00:54.880 --> 01:02.070 Okay so we are invisibles to your code and the first thing that we want to do is to create an API folder 01:02.280 --> 01:08.730 within our news application and then within this API for there we're going to create all the files that 01:08.730 --> 01:11.940 are going to be part of the API itself. 01:11.940 --> 01:18.720 This is not strictly needed but it surely is best practice and it helps us to keep the code much more 01:18.720 --> 01:21.300 that than it would otherwise be. 01:21.330 --> 01:26.860 So new folder I'm going to call it API. 01:27.070 --> 01:35.560 Now within DARPA for that the first file that I want to create is that she realizes that pi file which 01:35.610 --> 01:42.430 is the name clearly suggests is the file where we are going to define our serialized their classes considering 01:42.430 --> 01:46.990 that we are going to use a class that is provided by Django rest framework. 01:47.140 --> 01:50.020 We need to import to the module first. 01:50.080 --> 02:01.150 So from this framework import analyses and we can also take a look at the modules code so we can just 02:01.150 --> 02:07.960 right click go to definition and we've seen the module as you can see we can find some pretty useful 02:08.020 --> 02:09.010 comments. 02:09.010 --> 02:16.540 And the first line reminds us that as we've said Django this framework uses part of Django programming 02:16.540 --> 02:22.970 conventions making it really easy to understand as we can read and is read soon going to see realized 02:23.070 --> 02:30.070 that a sand Model C rises are similar to forums and model forums in Django and like forms they are not 02:30.070 --> 02:35.170 constrained to dealing with age the email output and form encoded input. 02:35.320 --> 02:39.670 I definitely suggest you take some time to take a look at the code. 02:39.680 --> 02:45.330 Did you your importing from every module normally did you're importing for the first time. 02:45.610 --> 02:52.300 Because especially if it is part of some well maintained an well known framework or package like Django 02:52.300 --> 02:58.360 or Django framework it's definitely going to be very well commented and it's going to give you a ton 02:58.360 --> 02:59.300 of useful tips. 02:59.350 --> 03:06.750 So let's get started by creating R C analyzer class and considering that we want to create these serialized 03:06.750 --> 03:08.910 that class for our articles model. 03:09.070 --> 03:20.770 We can just call it article sterilizer which is going to extend C realizes that serialize it so we've 03:20.770 --> 03:27.910 read that C that is the same forum setup very similar and in fact so much so that if we take a look 03:27.910 --> 03:34.730 at the general frameworks documentation about C realizes we've got a whole page dedicated to serialize 03:34.810 --> 03:42.100 their fields that are in fact very similar to what it was we would use we've Jane goes forms we have 03:42.100 --> 03:50.890 for example boolean fields we've got string fields so Chatfield for example he made field the lack field 03:51.160 --> 03:57.160 and just taking a first look at the menu here on the left we see that we also have numeric fields so 03:57.190 --> 04:03.380 integer flowed data and time fields choice Fields file upload and so on. 04:03.430 --> 04:11.170 So I definitely suggest you go and have a look at Django those frameworks documentation and maybe even 04:11.170 --> 04:17.040 save it among your bookmarks because especially the API guide becomes really handy. 04:17.050 --> 04:18.720 A lot of times. 04:18.910 --> 04:25.960 So for now let's go back to these what is your code and let's define the fields in this case from our 04:25.960 --> 04:26.550 model. 04:26.590 --> 04:29.440 Do we want to use we V not serialize it. 04:29.470 --> 04:34.150 So first of all the idea is the primary key of a given instance. 04:34.240 --> 04:46.980 So serialize this dot integer field and we have to pass the parameter read only peoples through because 04:46.980 --> 04:51.650 ninety nine point nine percent of the times we do not want to modify. 04:51.740 --> 05:00.690 JD Did yes to be a read only field so we've got the author which is going to be realized that dot char 05:00.690 --> 05:07.400 field and saving for the title the description. 05:10.010 --> 05:19.790 And even the body even though within our model stood by fire we specified body s text field that doesn't 05:19.790 --> 05:20.670 make a difference. 05:20.710 --> 05:27.970 E.R industrialize their representation then we've got location which is going to be Chatfield as well. 05:28.190 --> 05:40.110 Publication date which is going to be this time date field then we've got active boolean flag created 05:40.120 --> 05:53.560 at and updated at so active it's going to be boolean field White created at. 05:54.350 --> 06:04.790 Going to be eyes that start date time field we want to pass the read only parameter because this field 06:04.790 --> 06:10.700 is managed by Django automatically and you think you're going to do for. 06:12.000 --> 06:21.990 Updated at notice how no matter the kind of field that we're using would say I'm going to get a representation 06:22.000 --> 06:28.850 adjacent representation of the field thanks to the serialized field and see realize that a class there 06:28.880 --> 06:36.660 is no need to spend time thinking about how we might want to represent a certain field from our model. 06:36.660 --> 06:41.810 Because Django This framework provides us everything that we need. 06:41.820 --> 06:48.010 We working at a higher abstraction level where most of the troubles that we've come across in the previews 06:48.090 --> 06:49.760 section have been solved. 06:49.770 --> 06:50.610 The ready. 06:50.780 --> 06:55.050 And we can now focus only on what we want to do and to be fair. 06:55.050 --> 07:02.130 Now that we're actually working with a model there is even an easier way that we can use to actually 07:02.130 --> 07:04.050 see realize the data of our model. 07:04.530 --> 07:11.700 And that implies using debt Model C realize that a class that we're going to learn to use later on but 07:11.760 --> 07:17.610 for now it is important to work with debt serialize their classes in order to understand how the process 07:17.700 --> 07:19.320 really works. 07:19.320 --> 07:31.200 We now need to define two methods create method which accepts clearly self and validated data we can 07:31.200 --> 07:40.320 use the past instruction for now and then the update method which accepts self and instance and then 07:41.230 --> 07:49.020 as for create validated data as the name suggests they create and update. 07:49.020 --> 07:56.670 Methods are the ones that will provide the code to actually create and update instances for our article 07:56.700 --> 08:04.140 model and in fact considering that we're going to work with this model we actually need to import it. 08:04.200 --> 08:14.010 So from news that models import article and I'm also going to make a short note if your code gives you 08:14.010 --> 08:22.370 an error here in import a statement using the news dot models which is the up name dot up file. 08:22.470 --> 08:29.900 That's because you haven't used their projected folder as in fact the main folder here on the tree. 08:30.030 --> 08:33.900 I don't text editor doesn't give this kind of errors but B's words to your code does. 08:34.200 --> 08:39.370 So let's start by creating by writing the code for our create method. 08:39.420 --> 08:50.760 So we want to return article dot objects dot create and we're going to pass out validated data and let's 08:50.760 --> 08:54.840 also add a print just for now on validated data. 08:54.840 --> 09:00.930 We're going to take a look at the result of this print later on and this will give us an idea of what 09:00.930 --> 09:02.610 we really are working with. 09:03.430 --> 09:06.210 Now let's write the code for our update method. 09:06.240 --> 09:11.150 So we're going to update a specific instance that is passed. 09:11.640 --> 09:18.720 So let's start for example by the author field possibility that of course is we said I.D. read only 09:19.020 --> 09:23.360 so instead not auteur equals validated data. 09:23.380 --> 09:24.720 Dots get. 09:25.190 --> 09:32.800 We get author for instance not offer. 09:32.800 --> 09:37.010 Basically if there is no data that is being passed and just use the one did you already have. 09:37.780 --> 09:40.660 And then we going to do the same for all the added fields. 09:40.690 --> 09:53.280 So instance that title validated data and not get title or instance the title then instance that description 09:54.840 --> 09:55.230 yeah. 09:55.740 --> 10:00.480 And we're going to actually close this one for now. 10:00.600 --> 10:01.170 So Dan 10:04.460 --> 10:07.530 right below and it's rather effective to copy. 10:09.390 --> 10:11.220 Three more times two and three. 10:11.850 --> 10:20.930 So we got Buddy here and we've got location okay sure. 10:21.370 --> 10:26.980 Publication date and the active flac 10:29.950 --> 10:37.740 and in fact I can also indent the second parameter like so one to 10:40.870 --> 10:42.470 and I can open up the tweet. 10:43.500 --> 10:44.900 Once again. 10:45.090 --> 10:50.250 So now that we're assigning all the new values let's say to every instance we actually need to save 10:50.250 --> 10:50.390 it. 10:50.550 --> 10:55.830 We can just call the safe method of the ESM itself and we need to return it. 10:57.570 --> 11:00.310 So our articles utilize their classes almost daily. 11:00.300 --> 11:07.140 I see that there is multiple years not publication data but publication date. 11:07.170 --> 11:08.890 Same thing here. 11:09.150 --> 11:15.060 And now that our article serialized that a class is finally ready we can get to the details of our their 11:15.070 --> 11:21.410 serialization and the death serialization processes really work and we can do so from within. 11:21.420 --> 11:23.400 Jane goes interactive share. 11:23.440 --> 11:27.730 So I'm going to create a new terminal terminal new terminal. 11:27.820 --> 11:34.790 I'm going to go in expanded mode so Python managed that by Shell. 11:35.020 --> 11:41.250 And so first of all we need to import Beaufort article model and article serialize their classes. 11:41.400 --> 11:54.700 So from news dot models import article and from news that API that serialize its import article synergize 11:54.800 --> 11:55.550 said. 11:56.050 --> 12:01.350 Also considering that in the previous video we've actually created an instance of article we can now 12:01.380 --> 12:02.110 call it. 12:02.190 --> 12:13.540 So article instance equals article that objects that first and if we take a look at it now article does 12:14.010 --> 12:20.550 and we get the string representation according to what we've defined in Dan their string which means 12:20.550 --> 12:26.930 that we have the article author's name and then of course the article name itself. 12:26.940 --> 12:35.040 The article title and we can now serialize the data of these specific article instance thanks to our 12:35.100 --> 12:37.380 article realized that a class. 12:37.380 --> 12:46.590 So we do like so sterilizer equals article utilize it and we want to pass two articles serialized either 12:46.700 --> 12:57.570 article instance so article states like so and if you take a look at it see Eliza like so we see that 12:57.600 --> 13:01.420 we've got our articles utilize edit to which we best our article. 13:01.430 --> 13:07.680 Instance we see that we've basically converted all the fields that we've defined within that article 13:07.680 --> 13:12.000 serialize their class into python on native data type. 13:12.000 --> 13:19.920 We can take a look at them like sort utilize the DOT data you see it in how we got a dictionary with 13:20.070 --> 13:26.060 all the specific values of all the fields of our modeling instance. 13:26.700 --> 13:34.020 And so keep in mind that data are this data that we're going to see a lot during the course in the form 13:34.020 --> 13:41.970 for example of a request dot data represents the data that we're using at the serialize that we have. 13:41.970 --> 13:50.550 So in this specific case it represents clearly the data of our article instance I repeat we're going 13:50.550 --> 13:55.150 to see data a lot during the course that what it represents. 13:55.170 --> 14:02.790 It represents data to finalize the sterilization process we will enter the data into Jason and we do 14:02.850 --> 14:03.640 like so. 14:03.660 --> 14:06.630 So first of all we need to import adjacent renderer. 14:06.660 --> 14:19.570 So from rest framework that renders import Jason render. 14:19.740 --> 14:31.100 And now we can do like so Jason equals Jason Rand did it that render and to the trend that we pass serialized 14:31.100 --> 14:43.290 in that data we can now call Jason and we got our data rendered as you can see it's very similar to 14:43.290 --> 14:50.670 the one that we've actually seen here using realized that the data directly you can spot the difference 14:50.680 --> 14:58.260 for example in the active flag you see here we go through with the capital T as we do in Python Yeah 14:58.290 --> 15:07.750 we got active who if true that does not start with a capital T so let's not talk about this serialization 15:07.770 --> 15:16.260 process first of all we need to pass a stream into Python native data types so we need to make to import 15:16.920 --> 15:25.960 import I O which is just a Python module there to offer as an interface for handling data streaming. 15:26.220 --> 15:30.690 So now that we've rendered our data we actually need to pass it. 15:30.720 --> 15:32.930 So we need to import the parser. 15:33.120 --> 15:41.800 So from this framework important Jason pass it. 15:41.880 --> 15:48.370 And of course we get in there because it's not from the rest framework directly but it's from the rest 15:48.370 --> 15:51.940 framework that parsers import. 15:52.060 --> 15:53.510 Jason positive. 15:53.890 --> 16:02.410 So enter and so first of all we need to get our stream with the like so stream equals are your dot bytes 16:02.920 --> 16:06.350 are you Jason. 16:06.490 --> 16:10.100 And now that we've got our stream we can get our data back. 16:10.150 --> 16:24.630 Thanks to the bar set so data equals Jason power said no to pass the tweets we pass stream. 16:24.980 --> 16:31.670 So in doing so we're getting that data back and we can now use the articles serialize their class once 16:31.670 --> 16:41.300 again to restore their native data types into a dictionary of validated data like so that I zero equals 16:41.510 --> 16:44.520 article sterilizer. 16:44.660 --> 16:48.610 We need to pass data equals data. 16:48.620 --> 16:52.480 We need to check if the data is valid. 16:53.000 --> 16:54.410 Otherwise we will get an error. 16:54.440 --> 17:04.450 So serialize that dot is valid through perfect because of course our data is being processed properly. 17:04.480 --> 17:13.360 We can now actually ever look at it sort of see tonight that dot validated data. 17:13.470 --> 17:21.700 So our dictionary of validated data we can now save the city serialize there so serialize it that's 17:22.870 --> 17:24.020 safe. 17:24.040 --> 17:31.150 So now that we've called Save you see did we get back a new instance of article which is basically a 17:31.150 --> 17:37.870 clone of the one that we have previously created but that in fact is a new instance. 17:37.900 --> 17:49.900 So if we now do article not objects not all you see we've got quite he said we have two distinct instances. 17:49.900 --> 17:55.690 So now that you've talked in great detail about the sterilization and desalinization process let's talk 17:55.690 --> 17:59.060 briefly about process and Iran that is. 17:59.110 --> 18:05.020 So we've used the both of them in these examples and is therefore better to give a bit more of information. 18:05.050 --> 18:13.780 So a part of it is a class that allows the rest api to accept and comprehend different kind of requests 18:14.110 --> 18:21.130 and clearly Jenga this framework provides different kind of parsers one of which is Jason parser that 18:21.130 --> 18:29.800 we've used but we also have form a parser or multipart parser for example that can be used to send and 18:29.800 --> 18:33.610 receive images or other kinds of files. 18:33.610 --> 18:39.970 The really cool thing is that generates framework will automatically decide which kind of parser is 18:40.060 --> 18:48.640 optimal for a specific request based on the value this is associated with the content type header that 18:48.700 --> 18:56.570 as you might remember from the requests lesson is responsible for specifying the kind of content the 18:56.560 --> 19:04.420 type of content in fact that is used in the requests a renderer class on the other end allows us to 19:04.420 --> 19:09.740 provide different kinds of response or any way to personalize them. 19:09.760 --> 19:17.200 We have Jason renderer clearly but we also have template HDMI renderer or arousal API renderer and so 19:17.200 --> 19:18.100 on. 19:18.130 --> 19:19.530 So that was it for this lecture. 19:19.570 --> 19:20.810 I hope you enjoyed it. 19:20.860 --> 19:27.190 I remember you did you can download all the slides used in the course and that by the end of each lecture 19:27.190 --> 19:33.040 in the slides you're going to find a lot of reference links that will allow you to sensitively deepen 19:33.040 --> 19:37.930 your understanding of all the concept that we've been discussing in a specific lecture if you feel like 19:37.930 --> 19:38.660 so. 19:38.830 --> 19:40.230 So that was it for this video. 19:40.240 --> 19:41.740 See you in the next lecture.