Make your developer life a little easier with Django REST Framework's generic views.
[MUSIC] 0:00 At the end of the last stage I had created an API view to listing create courses, but 0:04 I don't yet have any way to do the rest of the normal crud operations. 0:09 Crud if you don't know stands for create read update and delete. 0:13 basically all the different operations you typically want to do on a bit of data. 0:17 Now I could keep using A.P.I. view and create a detail view to show just a single 0:22 course an update view to update one and a delete view to well delete one 0:26 that seems like a lot of work though especially since rest frame work gives me 0:32 a better way to handle this by using generic views Rest frameworks generic 0:35 views allow you to quickly build API views that map closely to your database models. 0:40 To give you an idea of how these views work, I'm gonna dive right in and 0:44 replace my list create course view with a generic view. 0:48 Okay, so let me see about using these generic views. 0:52 Now the first thing I need to do is I need to import the generics 0:56 which that will go up here at the top because I like to be all alphabetical, 1:01 rest framework and/or generic and since I'm going to be using the generics, 1:05 I don't need all of this stuff and I don't need this view that I created down below. 1:12 Now I know some of you like for me to leave the code and 1:18 maybe just comment it out so that you can refer back and forth. 1:20 It's a really bad habit to just comment out code and leave it there. 1:24 This is really where version control systems come in like GED or 1:27 some subversion like Mercurial. 1:31 So that you can go back and see older versions of your code. 1:33 Here on treehouse, 1:36 you can go look at older versions of the work spaces on previous videos. 1:37 It's a little tricky to get to. 1:41 You can also always check the downloads I include files for 1:43 every single video and you can always look back through those to 1:47 see exactly how the code was at the beginning or end of a particular video. 1:50 So, I'm going to delete these, they're gone gone forever. 1:54 You can never get it book, you can only get it back anyway. 1:57 I don't need those anymore. 2:00 So, I'm going to get rid of them and 2:02 I'm going to do the same thing on list create course because I don't need it. 2:03 We're going to replace it with something way way better. 2:06 So, if you want to see this check the older versions. 2:10 All right I'm going to get a new one and 2:13 I'm also going to call this list create course. 2:15 All right and generrick's, generics dot list create A.P.I. view. 2:19 So instead of extending A.P.I. view we're extending this 2:27 new generic view which is called The List create A.P.I. view. 2:30 Guess what this does it lets us list and create things in the API it so well named. 2:34 All right so I'm gonna give this two different arguments here two attributes 2:41 the first one is gonna be queryset which this is the queryset that it will 2:46 use in order to find the object or objects that it needs to list. 2:49 So the querryset will be models.Course.objects.all and 2:55 the serializer_class is the other one which this specifies what serializer 2:59 it should use in order to serialize the querryset that it receives. 3:04 So serializers.CourseSerializer all right and that 3:08 one's not instantiated because we want that to be evaluated every single time. 3:13 Okay that's it that's all the code I write for 3:18 creating a generic I've reduced my code from ten or so lines down to three lines. 3:21 The list create API view requires a couple class attributes like I said 3:27 But< everything else is done for me I don't have to do anything special and 3:31 I don't have any errors here when it restarted. 3:36 So, I didn't have to change my url or anything. 3:38 That still works that's still a view and 3:40 still has that same name and assuming I did everything right. 3:42 I should be able to come over here and refresh and get the exact same view. 3:47 And that looks the same. 3:53 Now I'm still logged in. 3:57 I should be able to come out here and put in a new one right, 3:58 submit a form and this is a little different. 4:02 So this is kind of cool. 4:05 By using the generic view, I don't just get that big 4:07 text box where have to type in like a blob of JSON, I can still do that. 4:11 I can come over here to raw data and I can type in you know whatever I want and 4:14 it helpfully pretty fills out the dictionary for me but 4:18 I also get this nice Django form that I can use to add in new data. 4:23 I don't have to type out raw JSON anymore. 4:29 That's cool. 4:31 While I'm here, 4:32 while I'm doing this I'm going to go ahead and implement the rest of the crud views. 4:33 I've got create and retrieve. 4:38 So now I need to do update and delete. 4:42 So I'm gonna go ahead and do that real quick, because it doesn't take very long. 4:44 So make a new class, which is RetrieveUpdateDestroy 4:49 course it's a long name generics dot retrieve 4:54 update destroy A.P.I. 4:59 view and amazingly that's still under eighty characters. 5:04 I can't believe it. 5:08 All right so again it the query set is going to be models.course.objects.all. 5:09 And it will on this list create one. 5:15 It kind of uses that whole query set to send out the data. 5:18 On this one, it uses the query set to go find the thing that you asked for, 5:21 like the primary key that you asked for. 5:26 And the Serializer class is exactly the same Serializers.courseSerializer. 5:29 All right now this name is a little bit longer but 5:34 I didn't have to write any extra code like nothing is new here right. 5:38 I do have to make a new or URL for this though so it's come back over here. 5:42 I'm gonna add a new one. 5:46 Yeah actually can go below. 5:48 That's fine and I'm going to do question mark P. 5:49 and these views the update retrieve, update, destroy, 5:53 they expect a key in the URL that's named pk. 5:58 So we're going to go ahead and give it a pk, and 6:04 then we'll do views.RetrieveUpdateDestroyCourse. 6:07 as_view and name of course_detail. 6:14 These are really long so 6:20 I'm gonna break these out onto their own lines because you. 6:21 Y'all know how picky I am about this stuff. 6:27 All right. 6:30 Comna there. 6:30 Save that all right so now refresh this and this still looks the same. 6:32 Let's go to slash one. 6:39 So, this should be me looking at Python basics because it has an idea of one. 6:42 And hey, what do you know there is Python basics. 6:46 So, that's pretty awesome. 6:49 So, since I put in that primary key I get just that single course. 6:51 And I'm logged in so I can delete this if I want I'm not gonna you know 6:55 what I will let's click delete I get a modal awesome. 7:01 So basically it pops this up just to make sure I actually want to really 7:04 delete this course I don't want to so I'm gonna hit cancel. 7:09 But I totally could delete it if I wanted to and then much like on 7:13 the creation side there is also a handy form right here that lets me update and 7:18 you'll notice that this button now says put instead of post. 7:24 Let's go back to here. 7:28 This one's post. 7:32 This one is put and that's because you put to update a method, or, not method. 7:33 The data resource and you post to create a new resource. 7:39 That's it! 7:43 I and you now have a fully functional set of crud views for course. 7:44 Rest framework comes with many different generic views. 7:49 So you don't have to use the long winded retrieve update destroy API view. 7:52 You could just implement the update API view or the destroy API view. 7:56 There are many different options to fit your needs. 8:00 I'll put a link to the documentation about generic views in the teacher's notes. 8:02 Okay take a little break, get some water. 8:06 And in the next video I'll add views for the review resource. 8:08 There might still be a few tricks up the generic sleeves. 8:12
You need to sign up for Treehouse in order to download course files.Sign up