WEBVTT 00:00.390 --> 00:05.340 So let's talk about the situation where someone wants to delete a particular post. 00:05.400 --> 00:09.870 They made a post, but they're saying, you know, I don't really want to do this anymore. 00:10.170 --> 00:11.430 This wasn't a great link. 00:11.850 --> 00:15.120 Whatever it is, let's provide the user that ability. 00:15.210 --> 00:22.350 So if we go to our views, dot pie, we just used a destroy model mix in to be able to add the ability 00:22.350 --> 00:24.260 to delete to an existing view. 00:24.270 --> 00:27.450 So we you might think, oh, well, let's do this for the post list. 00:27.480 --> 00:30.600 That way we can, you know, list, create and also do a destroy. 00:31.170 --> 00:38.310 But the issue is that are you are well currently if we go to you or else that pie for that particular 00:38.310 --> 00:44.970 view is for an API slash post and we don't specify any ideas for what it is that we're looking for. 00:45.720 --> 00:50.490 We do for the vote, but we don't want you know, if someone's using our API, be really confusing for 00:50.490 --> 00:55.730 them to say, you know, hey, if you know you're trying to delete, you use the vote or did the post 00:55.730 --> 00:57.090 and then you have to pass on a number. 00:57.090 --> 00:58.230 It be really confusing. 00:58.230 --> 01:04.650 What we really want is to say, you know, let's create API slash, post slash. 01:05.300 --> 01:10.680 And same thing that we got here, you know, someone passing some sort of I.D. and then we can either 01:10.950 --> 01:12.360 display information about it. 01:12.360 --> 01:15.270 Maybe they can update things about that particular post. 01:16.050 --> 01:18.000 You know, maybe they can delete things. 01:18.000 --> 01:23.640 You can decide what power you want to grant that user, but you've got to decide that. 01:23.670 --> 01:26.190 So let's go ahead and add this particular. 01:26.190 --> 01:28.800 You are l and then a view to back this up. 01:29.400 --> 01:35.610 So because this is a post and we're gonna go ahead and copy what we have for post lists and use a lot 01:35.610 --> 01:36.630 of what we have here. 01:37.280 --> 01:40.160 And you may be saying, OK, we don't want the list, we don't want to create. 01:40.170 --> 01:41.830 Do we just want to destroy? 01:42.210 --> 01:47.310 If we go ahead and look at the Django Rest. framework, if we go inside of the API guide. 01:47.370 --> 01:49.830 And again, this thing is a wealth of knowledge. 01:49.860 --> 01:53.880 But if you go to generic views, you'll see all the different things that we have here. 01:53.900 --> 01:59.160 So, for example, there's the list API view that we first used them the list and create. 02:00.020 --> 02:03.600 There's the destroy model mixin that we use before. 02:03.960 --> 02:08.480 And this one, I think this is the grandmaster, the retrieve update destroy API view. 02:08.520 --> 02:14.010 Basically, this says you can read something about a particular model. 02:14.280 --> 02:17.100 You can update something about it or you can delete it. 02:17.130 --> 02:21.450 And I think those are all valid things for what we want to do. 02:21.540 --> 02:24.820 So I'm going to go ahead and use this retrieve, update, destroy. 02:25.350 --> 02:30.360 And, you know, now this is sort of a decision here because, for example, on the actual Reddit, 02:30.420 --> 02:33.780 you can't update a post that you've created. 02:33.810 --> 02:35.850 You have to delete it and make a new one. 02:35.850 --> 02:39.390 If you want to change the title or the you are RL anything about it. 02:39.420 --> 02:45.540 And I think a lot of the important part of that is, you know, for example, if someone makes a really 02:45.540 --> 02:50.400 popular post and they have it going to a particular, you are well, you know, some sort of scammer 02:50.430 --> 02:55.680 could get something really popular and then switch out the you are l for something really nefarious 02:55.710 --> 02:57.510 right at the end there. 02:57.540 --> 03:03.510 So, you know, maybe it isn't a great idea to do the, you know, update, retrieve, update, destroy. 03:03.510 --> 03:05.850 You know, maybe we just want to use the retrieve, destroy. 03:06.270 --> 03:08.260 Now, I'm thinking this is actually a better move for us. 03:08.270 --> 03:09.840 So let's go ahead and copy this one. 03:09.990 --> 03:15.450 Retrieve, destroy K and we'll come back to our project. 03:16.400 --> 03:21.510 So instead of being this post list, I'm gonna call this post Retrieve, Destroy. 03:22.670 --> 03:26.720 And this is going to use generic stock, retrieve, destroy API view. 03:26.870 --> 03:30.110 So the query set is still going to be for post objects. 03:30.290 --> 03:33.650 We're still going to use the post serializer for this one. 03:33.710 --> 03:35.900 We want to make sure that it is authenticated. 03:35.930 --> 03:36.980 No read only just. 03:37.550 --> 03:44.060 Well, actually, that's not true because we do want someone to be able to see the details for a particular 03:44.060 --> 03:44.290 post. 03:44.300 --> 03:45.770 So we should keep that there. 03:46.450 --> 03:51.830 And because we don't need to do any sort of special thing, when someone deletes a post, we can go 03:51.830 --> 03:56.540 ahead and just delete this, perform, create we you know, you could go add the same sort of delete 03:56.540 --> 03:58.270 function here and do something really custom. 03:58.280 --> 04:00.620 But we don't need to go into that level of detail. 04:00.620 --> 04:03.130 It's all handled for us by the framework. 04:03.230 --> 04:03.800 So cool. 04:03.890 --> 04:06.050 Okay, so I'm going to copy the name of this post. 04:06.050 --> 04:08.690 Retrieve, destroy, save this file. 04:09.200 --> 04:14.180 Come back to our your URLs dot pi and say if someone goes to this post they can, you know, do a post, 04:14.420 --> 04:15.660 retrieve, destroy. 04:16.370 --> 04:22.730 And if we go back to our API and let's say I want to go look at this very first post, I'm gonna go 04:22.730 --> 04:26.400 do that very first post, slash one K. 04:27.410 --> 04:29.530 This is too sad for me to delete this. 04:29.540 --> 04:34.460 In fact, what I want to do is I want to log out of zappy code because that be code has made all the 04:34.460 --> 04:35.180 posts I'm going to do. 04:35.180 --> 04:36.850 One is Nick. 04:37.700 --> 04:39.260 Let me log him in here. 04:40.460 --> 04:43.310 I want to first go ahead and create a new post. 04:44.140 --> 04:45.260 So I'm going to say. 04:47.150 --> 04:55.850 Sports is what adding here, and this is going to go to HTP, colon slash, slash ESPN that come. 04:57.760 --> 05:04.090 Post that now if we go to our post lists, oh, look, there is, you know, sports from Nick, let's 05:04.090 --> 05:07.600 say I was like, I really don't like ESPN. 05:07.630 --> 05:08.980 You know, I want to share Yahoo! 05:08.980 --> 05:09.820 Sports something. 05:09.880 --> 05:12.250 I don't know what it is, and I need to delete this. 05:12.790 --> 05:15.530 Well, now I can go to see the idea of this. 05:15.550 --> 05:16.330 Number five. 05:16.360 --> 05:24.610 So I'm going to go to slash posts, slash five K and let's say I want to delete this. 05:24.820 --> 05:25.630 I hit delete. 05:26.500 --> 05:31.020 It gives me a tool for saying that it has deleted it if I go back to the post list. 05:31.320 --> 05:33.280 Oh look, it's completely gone. 05:33.370 --> 05:36.070 Now something that is an issue. 05:36.430 --> 05:42.040 Let's say I look at number three here and I say, oh, I should go delete whatever is number three. 05:42.040 --> 05:48.370 So I go to number three here and I hit enter and I say I'm going to delete this particular thing. 05:48.910 --> 05:52.090 Currently, I have the power to do that. 05:52.120 --> 05:52.350 Right. 05:52.390 --> 05:57.100 If I go back to the post, Lissa's is like, oh, OK, that's that's not what we're looking for. 05:57.460 --> 06:00.190 So I just told you, we didn't need an additional function. 06:00.220 --> 06:05.680 We actually do need an additional function if we go to have used up PI on a copy. 06:05.770 --> 06:08.510 What we have here for the delete. 06:08.660 --> 06:10.090 We'll go ahead and copy this. 06:11.100 --> 06:17.200 And on this post, retrieved Destroy will say, hey, if someone wants to delete this, we better make 06:17.200 --> 06:21.820 sure that they, you know, have access to this particular one. 06:22.000 --> 06:27.480 So what we're gonna do here is very similar to what we've seen before where we're gonna say, you know, 06:27.520 --> 06:31.000 hey, does this post belong to this particular user? 06:31.000 --> 06:38.710 So we're gonna say, you know, is there a post dot objects dot? 06:39.230 --> 06:47.050 And we want to filter and make sure that this particular post we're gonna pass in first, the primary 06:47.050 --> 06:54.040 keys are going to say P K is equal to the primary key that we get from the KW args. 06:54.040 --> 06:58.310 In fact, we can go ahead and just use that here, OK? 06:58.990 --> 07:02.760 Then we need to say that the poster is equal to. 07:02.800 --> 07:06.400 And we want this to be, you know, whatever users making this request. 07:06.490 --> 07:10.960 We're gonna go ahead and copy this pace that they're awesome. 07:10.990 --> 07:15.640 So we're gonna save this into a little variable called Post and then we'll basically do a little if 07:15.640 --> 07:16.210 statement here. 07:16.210 --> 07:22.570 We're gonna say if post DOT exists, meaning, you know, this thing is in the database. 07:22.600 --> 07:24.610 Well, then we want to do a proper delete. 07:25.120 --> 07:29.780 Now, we could go ahead and make this, you know, use what we did before where we, you know, grab 07:29.780 --> 07:31.770 the thing and delete it, then return response. 07:31.780 --> 07:34.600 There's actually a simpler way that we can do this. 07:35.260 --> 07:39.370 You know, the vote one was a little bit complicated here about how this all came to be. 07:39.370 --> 07:45.130 But with the particular post, we can just simply say return self. 07:45.220 --> 07:45.790 Dot. 07:46.720 --> 07:47.640 Destroy. 07:49.660 --> 07:49.970 OK. 07:50.090 --> 07:53.070 And we have to pass in the request. 07:54.270 --> 08:02.250 And the Star ERGs and also the star star KW ARGs. 08:02.650 --> 08:02.860 OK. 08:03.780 --> 08:06.430 So if it exists, then we'll do the destroying. 08:06.480 --> 08:10.860 This will give back the proper to form method or status code. 08:10.890 --> 08:13.140 I wanted to see the difference between those two there. 08:14.250 --> 08:19.740 But, you know, if that thing does not exist in the database, that means this post doesn't belong 08:19.740 --> 08:20.190 to them. 08:21.060 --> 08:24.410 So this is where we'd want to raise some sort of validation error. 08:24.900 --> 08:33.900 So I would copy this, come back and paste this and I'll say something like, this isn't. 08:35.490 --> 08:37.230 Your post to delete. 08:38.710 --> 08:39.270 Bre. 08:40.620 --> 08:41.160 There we go. 08:41.820 --> 08:43.470 They don't really tell him what's going on. 08:43.650 --> 08:45.900 Got to escape that single parentheses there. 08:46.050 --> 08:46.380 OK. 08:46.800 --> 08:50.450 So let's go ahead and save this and let's come back to our project here. 08:50.460 --> 08:51.420 So let's go to the post. 08:51.420 --> 08:51.880 Listen. 08:52.080 --> 08:58.010 I'm currently signed on as Nick and I see, you know, hey, I don't like this sappy code Web site. 08:58.080 --> 08:59.670 I'm going to delete this stinking thing. 08:59.870 --> 09:06.360 I might be saying to myself, OK, so I'm going to go to post slash one in here, even though I don't 09:06.360 --> 09:06.690 own it. 09:06.720 --> 09:12.510 I can view it, but if I try and go delete this and hit delete says, hey, this isn't your post to 09:12.510 --> 09:13.240 delete, bruh. 09:13.740 --> 09:15.450 But if I. 09:15.540 --> 09:22.320 Nick, go back and create a post like I will say, let's do search engine again. 09:25.990 --> 09:27.410 Google dot com k. 09:27.510 --> 09:29.710 So I've made this this has an idea of six. 09:29.800 --> 09:34.690 So I've go to post slash six K and I want to delete this. 09:35.110 --> 09:35.860 I hit delete. 09:36.280 --> 09:37.750 Boom, it is gone. 09:37.780 --> 09:41.200 It is no longer part of the posts. 09:41.470 --> 09:43.030 So there you have it. 09:43.180 --> 09:44.800 We have completed the project. 09:44.830 --> 09:47.980 I have a few things that I want to talk about as we wrap everything up. 09:48.580 --> 09:50.560 But you should be very proud of yourself. 09:50.590 --> 09:54.490 You just made your very first Django API is a really big deal.