WEBVTT 00:00.510 --> 00:06.960 Now, I believe the best way to learn is by doing so in this section to learn how to create an API with 00:06.960 --> 00:07.470 Django. 00:07.860 --> 00:11.590 We are going to be creating our own custom version of Reddit. 00:11.640 --> 00:13.790 Basically a simple version of Reddit. 00:14.100 --> 00:18.150 Now, if you don't know what Reddit is, Reddit dot com, it's basically a Web site where people can 00:18.150 --> 00:22.590 post links and then people can up or down vote those links. 00:23.220 --> 00:25.410 They can also comment on those links. 00:25.410 --> 00:25.620 Right. 00:25.680 --> 00:30.590 So you can see here at red dot com, there's a bunch of posts and this is the score of how many upvotes 00:30.600 --> 00:31.230 they have. 00:31.830 --> 00:37.530 And if we go into the comments, like it's going to show, you know, what people are saying about something 00:37.920 --> 00:43.350 right now for us to build something this complex, there's all these comments and things like that, 00:43.380 --> 00:45.210 it's probably a little bit out of our reach. 00:45.240 --> 00:52.500 But we can create a service that is going to allow people to submit links and be able to upvote them 00:53.250 --> 00:57.030 pretty quickly here using Django and making an API for it. 00:57.270 --> 01:03.300 So just so you know what it is that we're going for, that's the general aim of this course now, because 01:03.300 --> 01:04.920 this is a zappy code course. 01:04.980 --> 01:10.320 We're going to instead of calling it Reddit, we're gonna call it zap it to kind of see how that works 01:10.320 --> 01:10.530 there. 01:10.650 --> 01:10.960 OK. 01:11.400 --> 01:15.870 So let's go ahead and start a new Django project and open up my terminal again. 01:15.870 --> 01:17.880 If you're on Windows, it's gonna be the command prompt. 01:18.190 --> 01:22.800 And I assume you have some skills if you've worked with Django before moving around there. 01:23.310 --> 01:27.840 Remember, the basics are if you want to move around, c.D, and then the name of the folder that you 01:27.840 --> 01:28.740 want to go to. 01:28.740 --> 01:32.370 And if you do a c.D dot dot, that sort of sends you back one. 01:32.400 --> 01:36.390 There's a few differences between the command prompt and the terminal. 01:36.390 --> 01:41.610 But again, if you need any help with that, Google that or take my intro to Django course, my Django 01:41.610 --> 01:42.090 three course. 01:42.090 --> 01:43.080 That'll get you up to speed. 01:43.350 --> 01:43.590 All right. 01:44.160 --> 01:47.010 So with those caveats, let's go ahead and move to the desktop. 01:47.730 --> 01:57.540 And I'm going to start a new project by saying Django Dash Admen Start Project and I'm going to call 01:57.540 --> 01:58.470 mine again. 01:58.470 --> 01:58.990 Zap it. 01:59.010 --> 02:03.630 Feel free to call your Reddit clone, whatever he want, but I'm going to go ahead and hit enter there 02:03.630 --> 02:04.110 and look. 02:04.290 --> 02:06.300 I have this new folder called Zap. 02:06.300 --> 02:09.480 It's going to move into there and I say c.D. 02:10.230 --> 02:11.670 And then zap it. 02:12.750 --> 02:20.700 So once I'm inside here, I want to start a new app inside of here that's going to contain all the posts 02:20.730 --> 02:21.450 that people create. 02:21.480 --> 02:25.080 So that's going to be sort of our name for when people submit a link. 02:25.350 --> 02:26.810 And people can up or down vote it. 02:26.850 --> 02:28.110 It's going to be called a post. 02:28.350 --> 02:39.360 So I'm going to go ahead and say here that I want to do Python three managed PI Start app and I'm going 02:39.360 --> 02:40.630 to name this posts. 02:41.010 --> 02:47.570 So for you, depending on what install that you have, you may be typing Python three or you may be 02:47.820 --> 02:51.020 may just be typing Python managed up pie. 02:51.340 --> 02:54.270 Again, if you've worked with Django, you probably have some experience there. 02:54.360 --> 02:58.050 But now that we've done this, we should have this folder. 02:58.170 --> 02:58.800 Zap it. 02:59.070 --> 03:04.980 And inside of it, another folder called Zepa that contains like our settings, the posts that the settings 03:04.980 --> 03:05.370 all there. 03:05.370 --> 03:10.740 And then also this app that we just created called Posts, which has all the normal stuff like views 03:10.740 --> 03:12.120 and whatnot in models. 03:12.150 --> 03:14.490 This is where we're gonna make the model for our posts. 03:15.030 --> 03:17.100 That should all look feel fairly familiar. 03:17.130 --> 03:21.330 The one thing that I'm going to do is I always like to name the top level folders, something a little 03:21.330 --> 03:21.710 different. 03:21.720 --> 03:24.170 So that's different from this folder that I don't get confused. 03:24.180 --> 03:28.470 I'm going to call the SAP It project so I can quickly see, OK. 03:28.520 --> 03:32.880 This is the top level folder and then this is the one that has the settings and all those different 03:32.880 --> 03:33.420 pieces. 03:33.620 --> 03:33.850 OK. 03:34.470 --> 03:38.730 So now that that's all created, I can open up my text editor, which I'm gonna be using. 03:38.800 --> 03:43.440 Adam, feel free to use whatever text editor you'd like for this BLENKIN. 03:43.590 --> 03:46.900 Go ahead and open up what I've got on my desktop. 03:47.320 --> 03:47.480 OK. 03:48.670 --> 03:52.310 So I'm going to go ahead and open up these Zap It project. 03:54.000 --> 03:54.510 Awesome. 03:54.940 --> 03:55.270 OK. 03:55.440 --> 03:59.760 And now that I'm here, we need to start building out what this service is going to look like. 03:59.790 --> 04:03.790 So notice we're not even going to be touching any of the API stuff yet. 04:03.810 --> 04:06.270 First thing that we've got to cover is the model. 04:06.690 --> 04:13.290 How do we represent when someone wants to create a post like what really goes into a poster? 04:13.350 --> 04:15.370 Let's go ahead and start working here. 04:15.390 --> 04:23.850 We're gonna make a new class called Post and this is going to use the models DOT capital model. 04:24.040 --> 04:25.800 Same thing that was imported up here. 04:26.740 --> 04:28.440 So what goes into a post? 04:28.470 --> 04:31.170 Well, it's got to have a title. 04:31.260 --> 04:35.580 You know, people are going to have a you are réal for the post, but there should be a title, as you 04:35.580 --> 04:36.850 saw on the Reddit Web site. 04:36.870 --> 04:39.520 Like they may be saying, hey, check out this article that did it. 04:40.080 --> 04:42.000 Or look at this example of blah, blah, blah. 04:42.030 --> 04:43.710 And the You URL is different from the title. 04:43.760 --> 04:45.300 So we have a title. 04:46.200 --> 04:48.990 And this is going to be using our models. 04:49.860 --> 04:53.580 And it's going to be using a we're going to use a char field for this. 04:54.180 --> 04:55.680 So I don't want it to be too long. 04:55.680 --> 04:59.580 And I gonna say max length is equal to a hundred. 05:00.140 --> 05:02.880 This is just sort of an arbitrary choice by me. 05:02.910 --> 05:04.940 Feel free to do whatever you'd like there. 05:05.790 --> 05:07.890 A post has to have you or else. 05:07.950 --> 05:13.170 Let's go ahead and add that to something to say you are role is equal to models, Dot. 05:13.230 --> 05:15.030 And we're gonna use the u r l field. 05:15.540 --> 05:15.670 OK. 05:15.810 --> 05:17.910 We don't need to pass anything special in there. 05:18.510 --> 05:20.970 Then we need to know who it is that posted this. 05:21.000 --> 05:26.880 It'd be nice to be able display hey, this was posted by, you know, Nick or Joe, Sara, whoever it 05:26.880 --> 05:27.240 is. 05:27.330 --> 05:32.890 So let's go ahead and add another field here called Poster K. 05:32.950 --> 05:37.410 U arguably could call this user, but I like sort of the name poster. 05:37.420 --> 05:38.250 It makes you remember. 05:38.280 --> 05:38.610 OK. 05:38.910 --> 05:40.530 Who is the person who posted this? 05:40.540 --> 05:41.850 So that's why I called it poster. 05:42.320 --> 05:45.330 And this is gonna be a model's dot. 05:45.670 --> 05:50.910 And we need this to be a foreign key to foreign key. 05:51.930 --> 05:54.750 And what we're looking for is the class user. 05:54.840 --> 05:57.100 So we're gonna have to import this into our project. 05:57.120 --> 06:03.420 So I'm going to say from Django dot com Trib. 06:04.600 --> 06:07.150 Dot off dot models. 06:07.180 --> 06:08.830 Yes, I know this is a long one here. 06:10.120 --> 06:14.950 Models, import capital user. 06:15.580 --> 06:19.370 So now that we have that, we can go ahead and put this in here. 06:19.390 --> 06:20.170 Capital user. 06:20.170 --> 06:25.360 And then we have to specify, hey, if this is, you know, ever deleted, how do we want something 06:25.360 --> 06:27.790 that's referencing this post to happen? 06:27.790 --> 06:36.820 So we're gonna choose the on underscore delete to be equal to models, DOT Kaskade, the Cascade Delete 06:37.160 --> 06:37.490 gang. 06:37.840 --> 06:42.760 And the last bit that we need here is we just need to know when it is that this was created, when did 06:42.760 --> 06:46.980 someone make this post that'll kind of show some cool history on our Web site. 06:47.020 --> 06:56.320 So we're going to say created is equal to models, dot, date, time, field. 06:57.220 --> 06:57.570 OK. 06:58.420 --> 07:03.940 And we're gonna specify here that this is just set whatever the time is, whenever someone got to make 07:03.940 --> 07:05.590 sure I spelled out right date time field. 07:05.620 --> 07:10.290 We're just going to set it to be whatever the current time is when someone creates this or going to 07:10.290 --> 07:19.210 say auto underscore now underscore, ad and set that equal to true. 07:19.900 --> 07:20.150 OK. 07:20.800 --> 07:26.380 So now that we have this in place, this is what the post is going to look like. 07:26.440 --> 07:27.790 It's got these four attributes. 07:27.820 --> 07:31.960 And really, I think the most important thing is that whenever we see this, we should always have the 07:31.960 --> 07:32.920 most recent things. 07:32.920 --> 07:36.480 First, we're going to say class capital Medda. 07:37.060 --> 07:43.060 And I'm going to specify here that the ordering on this and we're gonna just do an array here is going 07:43.060 --> 07:44.500 to be a string. 07:45.130 --> 07:50.260 And then just do negative created that says, you know, list things by that early state that they were 07:50.260 --> 07:50.680 created. 07:50.690 --> 07:54.070 So you're going to say, when does this come into play here, this ordering? 07:54.100 --> 08:00.040 Well, when we list things out in the API and when we look on the admin portion of the Web site just 08:00.040 --> 08:01.660 to see what's inside of our database. 08:02.050 --> 08:05.790 This will help list things so that we have the most current things first. 08:07.030 --> 08:09.330 So that's all that's contained in the Post. 08:09.340 --> 08:10.630 You've worked with Django before. 08:10.630 --> 08:12.730 There shouldn't be anything too shocking here. 08:13.120 --> 08:16.810 The next thing is, you know, how do people vote on something? 08:16.840 --> 08:21.250 Well, we could just go ahead and have like a vote count, like and we could, you know, make this 08:21.250 --> 08:22.660 maybe like an integer field. 08:22.690 --> 08:27.760 But we want to make sure that someone can't vote for the same thing a million times. 08:28.120 --> 08:33.850 So if we're going to prevent that, we have to have a new class called vote that basically contains 08:34.120 --> 08:36.580 a reference to a user and he posts. 08:36.580 --> 08:42.520 So whenever once someone wants to vote on a post up vote post, we're gonna create a new vote object 08:42.580 --> 08:46.720 that says who the person who voted was and also what they voted for. 08:46.960 --> 08:51.820 And we can quickly count those whenever we're trying to get the sort of, quote unquote, score for 08:51.820 --> 08:52.270 a post. 08:53.080 --> 08:55.360 So there's going to be our second and last model. 08:55.390 --> 09:08.700 But we're going to say class vote is going to be inherent in four models, that Model K or door in there. 09:08.710 --> 09:14.320 And then we're going to specify that this has a voter, which the voter is essentially a user. 09:14.380 --> 09:17.680 So I'm going to copy almost exactly what we have up here. 09:18.110 --> 09:18.430 OK. 09:18.790 --> 09:19.600 Space this down. 09:19.600 --> 09:21.790 So the voter is a user. 09:22.210 --> 09:25.810 And then we need to know which post it is that they're voting for. 09:26.260 --> 09:28.070 So this is going to be pretty similar. 09:28.090 --> 09:33.340 It's still a foreign key, but instead of user, this is going to be post. 09:34.020 --> 09:34.230 OK. 09:34.840 --> 09:35.620 So there you have it. 09:35.680 --> 09:40.020 This is what the data model for our Zap It site is going to look like. 09:40.090 --> 09:40.240 So. 09:40.420 --> 09:42.100 So let's go ahead and save this. 09:42.550 --> 09:44.050 I'm going to move back to the terminal. 09:44.050 --> 09:47.820 And now that we've made changes to the models file, we need to make some migration's. 09:48.370 --> 09:51.410 So I'm gonna to say Python three managed up high. 09:52.120 --> 09:55.160 And I'm going to make migration's. 09:56.130 --> 09:56.440 OK. 09:57.580 --> 09:59.560 And it says no changes detected. 10:00.100 --> 10:02.620 I forgot that inside of our settings. 10:02.800 --> 10:05.510 We have to reference ah app that we created called Post. 10:05.660 --> 10:12.880 I come down to the installed apps and I'm going to add posts that make sure I had the trailing comma 10:12.910 --> 10:13.090 there. 10:13.090 --> 10:14.110 I'm going to save this. 10:14.620 --> 10:16.210 Now I'm going to run that same command. 10:16.210 --> 10:19.510 I'm going to hit the up arrow to do that and hit enter and look. 10:19.600 --> 10:22.240 Now it says, all right, here's the migration for your models. 10:22.270 --> 10:24.010 And then we want to then migrate that. 10:24.490 --> 10:28.040 So I'm going to change this to Python three managed up high migrate. 10:28.740 --> 10:29.010 OK. 10:29.740 --> 10:30.890 So there you can see it goes. 10:30.910 --> 10:32.390 Updates the database for us. 10:32.390 --> 10:34.330 So it's all ready to rock and roll. 10:34.780 --> 10:37.990 This was a very big first step for getting our project started. 10:37.990 --> 10:42.220 But we now have an awesome foundation and we can start building this thing.