Adding Search and Filters5:00 with Lacey Williams Henschel
Help yourself find things in the admin more quickly by adding some search and filtering functionality. We’ll add a search box to the top of the list view, and add some logical filters to a menu on the side of the page.
[SOUND] Welcome back. 0:00 Now that we've covered the basics of the admin and what it can do, 0:06 let's get into some details about the list view. 0:09 There are two basic pieces of the Django admin. 0:13 The list view, where you can see a list of 0:16 all the objects you've created in a particular model. 0:19 We can see all of our courses in a list for example. 0:22 Then there's the detail view, 0:26 where we can dive into the specifics of a particular object. 0:28 The detail view is also where we've been making changes 0:31 to the objects in our database. 0:34 In this stage, we're going to be dealing with the list view. 0:37 Our first step is to make the list view more helpful to us 0:41 by adding some tools to make it easier to find what we're looking for. 0:45 Once we have a lot of objects in our database, like several dozen courses and 0:49 several hundred quiz questions, 0:53 we won't be able to see everything at a glance in the list view like we can now. 0:55 So open up workspaces, and let's add some tools that will help us find what we need. 1:00 Go ahead and start your server, and log in to the admin. 1:06 As you can see, when we look at this list of courses, it's pretty easy to find 1:10 what we're looking for because there are only a few courses. 1:14 But some day, our site will have expanded a lot and will have a lot more courses. 1:17 So let's add a search box so that we can type in the title of a course or 1:23 a keyword and find our courses more easily. 1:27 This is really easy. 1:30 If we wanted to add a search field to the CourseAdmin, we just go to the class for 1:32 that model, CourseAdmin, and 1:36 add this particular list, it's called search_fields. 1:39 So we just type in search_fields, make it a list, 1:43 and then into this list we feed the names of the model attributes we 1:46 want to be searched when we search for a particular term. 1:51 So for courses, we probably want to scan the title, and maybe even the description. 1:55 Now we just save, and whenever we refresh this page we see this 2:03 awesome search box, and we can even test it out. 2:08 Like we can search for all of the Ruby courses, and we get back Ruby Basics. 2:12 Or we can search for all the Python courses and 2:17 we get back all of those courses. 2:19 You can add this search box for any list view in your admin. 2:22 Just add the search fields list and pass in the names of the attributes from your 2:25 model that you want to be scanned whenever you put something in the search box. 2:30 Let's add a search box to one more model. 2:34 What about the questions? 2:37 So down here under the QuestionAdmin we just type in search fields, and 2:39 for questions it's probably best if we 2:44 scan the prompt whenever we want to search for something. 2:48 So we save that, go back to courses, let's try these multiple choice questions. 2:51 And we see our handy search box. 2:56 This is really awesome, and 2:58 it's a really easy way to make your website a lot more useable. 2:59 Now let's add some filters to this side of the screen. 3:04 A filter is the kind of thing where when you click on it, 3:07 you can limit what you see to only items created in the last month, or 3:10 that only start with the letter A, or something like that. 3:14 Basically a filter just brings back a group of objects 3:17 that meet the criteria you specified in the filter. 3:21 So for our courses list, 3:24 let's add a filter that lets us limit by the date a course was created. 3:26 So here under course we just type in list_filter, 3:31 and then the only item in this list is going to be the field created_at. 3:38 Now we go back over to courses, and there on the side we can see our filter. 3:44 So whenever we click on something, for example, today, 3:50 we can see that Ruby Basics is the only course that was created today. 3:53 If we go back to any date we can see all of our courses. 3:57 Let's add the is_live attribute to the filter too, so 4:01 that we can see all of the courses that are currently live on our website. 4:04 So we just add is_live to this list and save it. 4:10 Now when we refresh, we see is live down here. 4:16 So we can see that Ruby Basics, Python Testing, and Python Basics are all live. 4:21 Now we can stack these filters. 4:27 So we can say, for example, that we wanna see the courses created today 4:29 that are also live, and we see we only get back Ruby Basics. 4:34 If we wanted to see all the courses that we created in the past month 4:38 that are not live, we don't have any results 4:44 because we don't have any courses that fit both of those criteria. 4:47 Pretty neat? 4:51 Before the next video, why don't you try to think of some 4:53 other filters that might be useful additions to your site? 4:56
You need to sign up for Treehouse in order to download course files.Sign up