Django: Getting Started Transcripts
Chapter: Django Admin
Lecture: Add computed fields to 'ModelAdmin' classes

Login or purchase this course to watch this video and the rest of the course contents.
0:00 The list display attribute can indicate any field in the model or a colorable in the admin model.
0:08 Whatever that cullable returns is what is shown for the row. This is how you can get at things like the author's data inside of the books row.
0:17 I'll just add a new item to the couple called author last name. And this is the format of the callable that the admin model expects.
0:34 It takes an argument which is the object that is being printed. This method will be called once for each row in the change list screen,
0:42 with the object that corresponds to the row as a parameter. Any string return from this method will be shown on the change list screen.
0:52 I'm returning the book's author's last name. Now back to the browser to see this in action. Refreshing the page and there it is.
1:01 Each book now shows the author's last name. Notice that you cannot sort on this column. The sorting isn't done in the web interface.
1:11 It is a call through to the back end which changes the result of the underlying SQL query.
1:17 As you can return absolutely anything in the callable column method Django has no way of knowing how to change the database query.
1:24 The admin uses pagination when you have a lot of records. This pagination is directly linked to the SQL query.
1:32 If you have a lot of records sorting them by that callable column would mean loading all of them into memory. Django doesn't want you to do that.
1:41 So it sticks with using the database query for this kind of stuff.

Talk Python's Mastodon Michael Kennedy's Mastodon