Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Customizing What You See2:41 with Lacey Williams Henschel
The default for the list view is to just show you the main piece of information about your objects. In our case, we see the course titles, for example, but not much else about the courses. In this video, we’ll learn how to add other fields from our models to the list view, to give us more information at a glance.
You've probably noticed that the default for the list view is to show you whatever 0:00 is in the string, or underscore underscore string underscore underscore method of 0:04 your mode;, but it doesn't show you anything else. 0:08 So for our courses list we only see the title of the course, 0:11 because that's what we have set to return in the string method. 0:15 But being able to see more fields from our models can be really helpful for 0:19 a lot of reasons. 0:23 It can give us more things to sort by. 0:25 Date, or whether the course is live for example. 0:27 It let's us see more information at a glance, and 0:30 it can even help us edit information about our courses without having to navigate 0:34 all the way to the detail view, saving us some clicks. 0:38 For now, let's focus on adding some more fields to the list view. 0:42 As I've said before, 0:47 the great thing about the Django admin is how much it gives to you for free. 0:49 To add more fields to our list view, we just need to tell the admin class for 0:53 that model that we'd like to see those fields. 0:58 So for courses, it might be nice to see, for 1:01 example, the date the course was created. 1:04 To see that, we just add the list_display variable, and 1:07 make it a list, and then we add the names of the fields we want to see in order. 1:13 So we know we wanna see the title because we already see that, but 1:18 we wanna add the created_at field so we can see the date. 1:22 So if we navigate to courses, 1:26 now we see we can see the date that the course was created. 1:28 And then this table became sortable too, so we can actually click on created at and 1:33 we can sort In ascending order by the date of course was created, or 1:37 we can sort in descending or reverse order. 1:41 Or we can just put things back in alphabetical order, 1:44 it doesn't really matter. 1:46 Now let's add the is_live field two so 1:50 we can see at a glance which courses are live on our site. 1:52 So to do that we just add this other field into our list display. 1:56 Now we can see this red X, which means that the course is not live, 2:04 versus this green checkbox, which means the course is live. 2:09 And again, we can sort by this too. 2:13 Pretty neat? 2:17 Remember that in order for the is_live field to do anything, you would need 2:19 to add some code to your view to only bring back courses where is_live is true. 2:24 I'll let you do that on your own. 2:29 But isn't adding more fields to your list view simple? 2:32 Before you try the next video, 2:35 why don't you add some more fields to the other list views? 2:36
You need to sign up for Treehouse in order to download course files.Sign up