Django: Getting Started Transcripts
Chapter: Django Admin
Lecture: Improving the Author model's list display

Login or purchase this course to watch this video and the rest of the course contents.
0:00 And back in catalogs file. I've imported a couple of new functions here that I'm going to need in the code
0:13 Now I'll add a list display attribute to the author. change list to make it more useful. I've added references to the attributes in the model for id,
0:28 last name, and first name and that last reference show books is going to be a callable,
0:37 just like the callable author last name in the book admin class, this takes an object as an argument.
0:44 This will be called once for each author in the change list. When the change list page is displayed,
0:53 I was raised by a kindergarten teacher so there's a voice in the back of my head that yells whenever I see the wrong plural on a word.
1:00 The first thing I've done here is to get a count of all the books associated with this author so that I can properly quiet that voice.
1:09 You've seen count used before with the query manager but on its own when used on its own, it is the equivalent of counting all of the objects.
1:18 You can also call count after a query which returns just the number of items in that query result.
1:26 Programmatically this is equivalent to getting the query reset result and calling length on it. But this is actually a lot faster.
1:34 The query manager sees the use of the count call and uses it when formatting the underlying SQL. What you get back from the database is just the count,
1:44 which is way faster than getting back all the objects and then counting them yourself, with a call to length.
1:51 The filter in this line is all the books that have a foreign key pointing to the author object passed into this call.
2:02 The reverse function is short for reverse look up and although it probably could have been named better, this is the function that looks a URL by name,
2:12 That hideous string there is what the change list page for book objects in the Django admin is named.
2:20 The admin colon part is a name space for the admin module and then the format
2:25 is module name, model name admin function which in this case is change list. When an admin model is registered,
2:35 Django automatically creates all the associated URLs and names them using this format. Reverse returns a string with the URL in it.
2:45 The Django admin recognizes some query parameters to help you filter what is on the change list screen.
2:51 The chunk at the end of this line is me saying please filter the book listing showing only those books whose author's foreign keys
3:00 ids match my author objects ids. If you were paying close attention when I demonstrated the filter mechanism you'll have seen the same thing.
3:09 This is how it works, when I clicked the author filter it was using this same kind of parameterized URL.
3:19 A quick variable to represent singular or plural books. Django has a bunch of security stuff built in to prevent you from accidentally injecting tags
3:31 into your HTML. If your author's last name has a greater than sign in it the author last name callable would seriously mess up the page.
3:41 To avoid this problem, Django automatically escapes all text output to the template engine. Except in this case you don't want it to be escaped.
3:50 You want it to insert an actual link. Enter the format HTML function. This works like a call to string format but marks
3:59 the string is safe to Django so that it isn't escaped when it is rendered. Like the string format function,
4:06 you use brace brackets as placeholders and then the arguments you pass it, fill those in.
4:12 There are other ways of accomplishing the same thing but this is the one that takes the least amount of code. Alright, so let's recap this function.
4:22 This is a callable column that will show a link for each author. If you click the link, it will go to a book change list page with a filter turned on.
4:34 The filter, filters the authors whose object got clicked. The display text for this link will show the number of books for the author and
4:44 most important to my mom, the plural will be correct. Off to the browser to check this out.
4:50 This is the author change list page and look at those pretty links. Homer has two books and Dickens has three.
4:58 Click one of them and there are all of the Dickens books.

Talk Python's Mastodon Michael Kennedy's Mastodon