WEBVTT 00:00.540 --> 00:05.190 Now, as we kick things off of working with this jingle rest framework, the first thing that we're 00:05.190 --> 00:12.720 going to talk about is serializer is now serializer are a way to sort of be a middleman between models 00:12.810 --> 00:13.860 that we have in our project. 00:13.920 --> 00:14.280 Right. 00:14.610 --> 00:21.780 In this case, posts and vote and also the API, they sort of translate, you know, if this is a Django 00:21.780 --> 00:27.240 model, how do we turn that into a Jason object and how do we do it go? 00:27.240 --> 00:27.870 Vice versa. 00:27.900 --> 00:28.140 Right. 00:28.170 --> 00:33.980 If someone passes us some, Jason, that they want saved as a Jenko model, how do we do that communication? 00:34.020 --> 00:35.850 It's using a serializer. 00:36.000 --> 00:39.150 So what we're gonna do is move over to our post app. 00:39.330 --> 00:40.140 This folder. 00:40.680 --> 00:43.140 And let's go ahead and create a new file here. 00:43.140 --> 00:43.650 So I'm going to right. 00:43.650 --> 00:50.780 Click new file and I'm going to call this serializer as serial Kaisers dot pi. 00:51.120 --> 00:51.370 OK. 00:52.140 --> 00:53.350 So we'll go ahead and save this. 00:53.370 --> 01:00.390 This isn't doesn't have to technically be this name, but this is what the Django Rest framework suggests 01:00.390 --> 01:01.250 for the name of this file. 01:01.260 --> 01:07.050 So it's good to sort of follow their styling as you get to building out your project. 01:07.470 --> 01:10.210 And the first thing that we need to do is import some other code. 01:10.250 --> 01:15.320 So we're going to say from Rest., underscore framework import. 01:15.800 --> 01:17.700 And we want cereal. 01:17.820 --> 01:18.650 Isaiah's. 01:19.960 --> 01:26.040 OK, so now that we have that, we have to create a serializer for each model that we're working with. 01:26.060 --> 01:29.360 So let's go ahead and start first with the post model. 01:29.510 --> 01:33.470 So since we're going to be working with this class, we have to import it. 01:33.920 --> 01:35.870 So let's go ahead and import that up top here. 01:35.870 --> 01:38.640 We're going to say from Dopp models. 01:39.700 --> 01:45.790 Import, we want capital posts and we have the DOT models because we're staying in the same folder here, 01:45.790 --> 01:46.690 the same app. 01:47.330 --> 01:47.590 OK. 01:47.890 --> 01:52.290 So with that in place, we're gonna create a class, which is the way that we make a serializer. 01:52.300 --> 01:56.520 We're going to say class and we're gonna give a name that describes the serializer. 01:56.530 --> 01:58.710 It's a it's for our post objects. 01:58.720 --> 02:01.570 So we're gonna call it post serializer. 02:02.110 --> 02:06.520 Now, it doesn't have to be this name, but I think it's kind of, you know, describes what's going 02:06.520 --> 02:08.410 on here inside of the parentheses. 02:08.710 --> 02:15.310 We're gonna have to say serializer is dot and we want capital model. 02:16.410 --> 02:22.200 Serializer, now, there's all different kinds of serializer is out there, if you go ahead and check 02:22.230 --> 02:26.010 the Web site for the project, you can see all the different ones out there. 02:26.070 --> 02:31.200 But we're going to use the model serializer, which basically makes it really easy to say to take some 02:31.200 --> 02:36.660 model that we have and be able to translate it into Jason or whatever else we need in our API. 02:36.900 --> 02:40.410 So with this, what do we add into the class? 02:40.800 --> 02:46.750 Well, the first thing that we have to do is say, what fields do we want associated with the API? 02:47.370 --> 02:49.940 And if we go ahead and look back at our model, right. 02:49.950 --> 02:54.360 Like a post has a title, it has a your real poster when it was created. 02:54.720 --> 02:59.100 These are probably all things that we want in there, but there's going to be some additional pieces 02:59.100 --> 02:59.520 as well. 02:59.550 --> 03:01.380 We want to know what the vote total is. 03:01.440 --> 03:06.180 Now, even though that isn't part of the model here, we'll come back and add that later. 03:06.630 --> 03:08.190 But it is something to think about. 03:08.220 --> 03:14.550 So the serializer is basically a way that you can connect your fields and have some additional properties 03:14.550 --> 03:16.950 that you want showing up inside of your API. 03:17.070 --> 03:19.290 But for the time being, let's take the basic fields. 03:19.290 --> 03:24.270 I'm going to copy these four lines, bring them into this project just so that I you have a reference 03:24.270 --> 03:25.290 of what I'm looking for. 03:25.530 --> 03:28.830 So I'm going to type out class meta. 03:29.850 --> 03:35.400 Colon and then in here have to specify what model I'm basing the serializer on, so I'm going to say 03:35.400 --> 03:40.260 model is equal to capital post, which we just imported up there. 03:41.160 --> 03:42.270 Post serializer. 03:42.390 --> 03:42.930 There we go. 03:43.440 --> 03:47.430 And then I have to say what fields I want included with this. 03:47.520 --> 03:51.120 So this is just looking for an array of strings that's gonna be the name of the fields. 03:51.130 --> 03:52.650 We're gonna start first with I.D.. 03:53.040 --> 03:59.010 Now, IDEA isn't explicitly listed as a field, but it's included with every Django model. 03:59.100 --> 04:00.240 So we have that there. 04:00.630 --> 04:02.480 And then I'm just gonna start using what we have here. 04:02.490 --> 04:04.020 So I'm going to copy title here. 04:05.350 --> 04:06.250 Add that in. 04:07.960 --> 04:10.090 Then going to add you URL. 04:11.610 --> 04:13.710 K poster. 04:14.580 --> 04:16.170 Go ahead and put that there. 04:16.860 --> 04:18.630 And also created. 04:20.090 --> 04:20.480 Great. 04:21.050 --> 04:29.840 So this is essentially saying, you know, when someone wants to get some sort of post from our database, 04:29.930 --> 04:35.060 it's going to come out as a model, go through the serializer and eventually turn into Jason. 04:35.210 --> 04:37.940 And this is literally all we need to get started here. 04:38.270 --> 04:39.560 So we'll go ahead and save this. 04:39.650 --> 04:40.040 All right. 04:40.490 --> 04:42.230 So we've got our serializer here. 04:42.620 --> 04:46.430 Let's now move to creating our very first API. 04:46.520 --> 04:48.530 You are l oh, this is so exciting. 04:49.160 --> 04:55.970 So I'm gonna go ahead and move over here to my you URL's and we're gonna be adding a new path so you 04:55.970 --> 04:58.700 can decide how you where you want your API to go. 04:58.730 --> 05:05.330 I think a really nice way to do this with most Web sites is to started out with API and then slash whatever 05:05.330 --> 05:07.530 sort of resource it is that you're going for. 05:07.550 --> 05:11.870 So, for example, we're trying to get the posts shown to the people. 05:11.870 --> 05:16.420 So I'm going to say API slash posts, OK? 05:17.600 --> 05:23.240 And with that, I'm also going to now say where this is going to, which means we're gonna have to import 05:23.240 --> 05:24.740 the view from our posts. 05:25.370 --> 05:29.570 We're going to say from posts import. 05:31.970 --> 05:32.690 Views. 05:34.250 --> 05:36.600 So now I can say views, Dot. 05:37.070 --> 05:41.330 And then I need to get the name of the function or class that we're gonna be using. 05:41.450 --> 05:45.830 Now, with the Jango rest framework, you can use either functions or classes. 05:45.860 --> 05:50.570 But I think the framework really shines when we're using classes. 05:50.840 --> 05:51.890 So now's the point. 05:51.910 --> 05:55.790 We're gonna go over to our views and create our very first view. 05:56.210 --> 06:00.890 Now, there's lots of things that we want to do with this API, but I'm gonna start first with just 06:00.890 --> 06:07.100 the ability to list out all the different posts that are saved inside of the database. 06:07.280 --> 06:09.170 So I'm going to make a new class here. 06:10.210 --> 06:13.130 And I want this to be a post list. 06:13.160 --> 06:18.350 It's going to list out all the different post objects so I can do my parentheses here and a colon and 06:18.350 --> 06:19.700 inside of the parentheses. 06:20.120 --> 06:25.910 We have to pass in some code and we've got to import this from the framework, so to say, from rest. 06:26.030 --> 06:27.440 Underscore framework. 06:28.330 --> 06:30.040 And I want to import. 06:31.820 --> 06:33.710 All lower case generics. 06:34.070 --> 06:34.300 OK. 06:34.910 --> 06:39.940 So with that in place inside of the postals parentheses, we're going to say generics dot. 06:40.520 --> 06:46.300 And then we want capital list, capital API, capital view. 06:47.000 --> 06:47.390 All right. 06:48.050 --> 06:53.430 Now, what this is doing is it saying, OK, we're going to make a class based view. 06:53.870 --> 06:58.340 And it's going to be listing out information that's going to help us do that very quickly. 06:58.790 --> 06:59.720 Here's what we've got to do. 06:59.750 --> 07:04.910 We first got to say, you know, what is it that we're hoping to display in the end? 07:04.940 --> 07:07.040 What do we what are we pulling from the database? 07:07.050 --> 07:08.420 And that's called the query set. 07:08.780 --> 07:12.620 So I'm gonna type here all lowercase query set. 07:13.210 --> 07:20.300 I say this is equal to capital post dot objects, dot all the parentheses. 07:20.720 --> 07:23.270 Now, in order to use post, we've got to import that. 07:23.300 --> 07:29.510 So again, I'm going to say from DOT models, I want to import capital post. 07:30.390 --> 07:32.220 OK, so that's the query set. 07:32.250 --> 07:34.290 That's what we're looking to pull from the database. 07:34.620 --> 07:40.020 And then we have to specify what serializer we're going to use for this particular view. 07:40.440 --> 07:44.610 And remember, we just created one the post serializer. 07:44.610 --> 07:46.080 So I'm gonna go ahead and copy this name. 07:46.080 --> 07:46.530 So I have it. 07:46.530 --> 07:47.160 Exactly. 07:47.760 --> 07:57.300 I'm going to come back and say that the serial serializer underscore class is equal to the post serializer, 07:57.360 --> 07:59.040 which again, we've got to import this. 07:59.070 --> 08:06.330 So let's say from DOT Serializer is import the post serializer. 08:07.110 --> 08:11.550 And with this in place, this is all we need for our very first API call. 08:11.820 --> 08:13.290 Yes, this is it. 08:13.530 --> 08:13.770 OK. 08:14.070 --> 08:16.250 So I'm going to save the Vieuxtemps pie. 08:16.420 --> 08:19.410 I'm going to copy the name of our class based view here post list. 08:19.890 --> 08:20.670 I'm going to copy this. 08:20.670 --> 08:25.440 Come back to your URLs and say if someone goes to API slash posts, send them to views. 08:25.530 --> 08:29.160 Dot post list dot as. 08:30.920 --> 08:31.760 Underscore. 08:31.820 --> 08:34.760 View with the parentheses like that. 08:35.150 --> 08:37.880 And this is all we need to get started. 08:38.080 --> 08:38.270 OK. 08:38.660 --> 08:40.340 So I'm gonna go ahead and save this. 08:40.730 --> 08:49.250 Let's go ahead and move to our terminal and run our project song to say Python Managed Pie runs server. 08:50.460 --> 08:52.310 OK, so this is now up and running. 08:53.140 --> 09:00.260 And you can see it's here at Port eight thousand, so I'm going to copy that in Safari and I'm going 09:00.260 --> 09:01.790 to plug that in. 09:02.030 --> 09:04.280 And here you can see we've got nothing set up for the home page. 09:04.310 --> 09:04.910 That's fine. 09:04.960 --> 09:09.680 But look what happens if we go to slash API slash posts. 09:10.210 --> 09:11.060 Look at this. 09:11.240 --> 09:12.260 We get something. 09:13.130 --> 09:15.670 It kind of says, OK, you know, Jenko rest framework. 09:15.670 --> 09:20.450 You're thinking, OK, this is kind of cool, but there's there's no information inside of here. 09:20.660 --> 09:26.870 Now, the reason for this is because we don't have anything saved into our database and we've got to 09:26.870 --> 09:27.800 do something about that. 09:27.820 --> 09:33.920 So in the next video, we're going to talk about how we can add some information into the database here.