With many records comes long wait times, or something like that.
There is one more thing I need to do to make sure the API doesn't have to parse 0:00 thousands of reviews in a single API request, pagination. 0:04 Rest framework provides some built in pagination that will work automatically 0:08 with generic views and view sets. 0:11 You implement a global default by setting the default pagination in 0:14 the settings file. 0:16 If you need to, you can get very explicit and 0:18 set pagination directly on your view sets or generic views. 0:19 All right, so I think it's probably a good idea to set up the default pagination 0:24 first and that gets done in settings.py. 0:28 So you remember down, way way down here at the bottom, so much scrolling, 0:31 there is this rest framework dictionary where I've set all of my arguments. 0:36 So I'm gonna add a new one here which is default pagination, 0:42 pagination_class and notice this is class not classes. 0:47 You only get one default pagination class. 0:52 There's not like a fallback. 0:54 So rest_framework.pagination.PageNumberPagin- 0:56 ation. 1:03 And that's just a little over 80, so I'm gonna break it. 1:04 Nah, I'll leave it, I don't care. 1:09 All right, and then page size. 1:12 Which is how many items are on a page. 1:17 I'm going to set that to five. 1:19 Some of you may have been shocked by me saying, I don't care, yeah just welcome to 1:22 my world where sometimes it just doesn't matter if it's a little over 80. 1:27 All right, so the first one of these sets the pagination class, 1:30 which is the class that's going to be used to figure out how to break things into 1:34 chunks of a particular size. 1:39 So into pages and 1:41 then the page size says how many things are going to be on each particular size. 1:42 Okay? I'm using 1:47 a small number cuz I don't have a lot of stuff in my database here. 1:48 You would probably use something like 10, or 50, or 100 on your own stuff. 1:51 So just keep that in mind. 1:56 So, I'll refresh this, and the content here has now changed. 1:59 So instead of it just being a list of all these items, 2:05 we now have the list of the items inside a key called results. 2:09 We have a count, which is how many things there are. 2:14 And we have next and previous, which would be URLs to the next and 2:17 previous pages of results. 2:21 I can show you that real quick by changing this, to one. 2:23 Refresh, there we go, and my next page would be here at page two. 2:29 And if we go to page two then I have a next page at page three and 2:34 I have a previous page at slash courses and 2:38 you can also assume this had a question mark page equals one. 2:40 So kinda cool, kinda nice. 2:44 I'm gonna put that back to five. 2:47 All right so that's fairly straightforward and handy. 2:47 Now the default pagination gets applied to all generic views and 2:53 view sets and all that kind of stuff in the project. 2:58 So, just keep in mind any changes you make to the default pagination 3:01 potentially will affect all of the pages of your API. 3:05 The default pagination won't automatically work on your ad hoc views, 3:08 so like this, let me get to it, 3:14 this reviews view that we gave here, this detail route that won't get paginated. 3:18 You'll have to make your own calls to the paginator to handle that. 3:24 Since I don't have a lot of reviews yet, 3:29 I'm also going to change the pagination size on this one to one. 3:31 Let's let's see how to do this custom pagination. 3:34 So inside here we have all of our normal stuff. 3:39 Nothing has changed yet. 3:43 So here I'm gonna do self.pagination_class.page_size 3:44 is gonna be equal to one. 3:51 So this pagination class was set by that setting that I set awhile ago. 3:54 The view knows what the pagination class is and what its page size is. 4:00 By default the page size is five. 4:05 I'm setting that to be one. 4:07 So now I want to do reviews equals model.Review.objects.filter. 4:10 Course_id = pk, so get all the reviews for 4:15 the current course and then I'm gonna 4:21 do page = self.paginate_querryset, 4:27 that's what it is, reviews. 4:32 If page is not None, so so long as I have a page. 4:37 Serializer equals serializers.reviewSerializer page and 4:41 many is equal to true because it's probably more 4:47 than one thing on there, right? 4:51 And then I'll return self.get_paginated_response. 4:55 Wow, some days, serializer.data and otherwise, 5:03 if that somehow falls through, then serializer = serializers. 5:09 Yeah, that's what I want to do right there. 5:18 Serializer = Serializers.ReviewSerializer, and I want to give it reviews, 5:21 and many=True, 5:26 because we've got all of those reviews coming through. 5:31 All right, so I changed it a little bit. 5:35 So let's go through what I did here. 5:38 So I set the paginater to have a page size of one. 5:41 Because I want just one item on a page. 5:45 I want to grab all the reviews and I'm going to paginate that query set. 5:47 Assuming that I have some pages then I'm gonna send this out through. 5:52 If, somehow, I don't have like pages or whatever, then I'm just going to 5:57 serialize all the reviews that I do have and I'm going to return a response. 6:02 So now let's try going to one slash reviews, 6:06 and if I look, I still have count next previous and results right, 6:12 because the reviews are now being paginated just like the courses were. 6:16 So that's cool. 6:21 There is more than one option and style for pagination rest framework. 6:23 You should, you know, check out your options, you get to know them all. 6:27 So check the teacher's notes and 6:30 you'll find a link to the pagination documentation right down there. 6:33 Pagination is a great way to reduce the amount of strain on your A.P.I. 6:37 due to large records or lots of related records. 6:41 At this point you have a strong customizable A.P.I., that's easy for 6:44 users to get the data they need. 6:47 Now, though, it's time to think about securing this A.P.I. 6:50 because the wild internet isn't nearly as nice as I wish it was. 6:52
You need to sign up for Treehouse in order to download course files.Sign up