Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Modifying a Change List

00:00 Modifying a Change List using list_display.

00:05 Implementing .__str__() is a quick way to change the representation of a Person object from a meaningless string to understandable data.

00:14 Since this representation will also show up in drop-downs and multi-selects, you definitely want to make it as easy to understand as possible. You can customize change list pages in far more ways than just modifying an object’s string representation.

00:30 The list_display attribute of an admin.ModelAdmin object specifies what columns are shown in the change list. This value is a tuple of attributes of the object being modeled. For example, in core/admin.py, modify PersonAdmin as follows.

00:55 The code you’ve just seen modifies your Person change list to display the last_name and first_name attributes for each Person object.

01:04 Each attribute is shown in a column on the page. As you can see, the two columns are clickable, allowing you to sort the page by the column data. The admin also respects the ordering attribute of a Meta section.

01:22 Adding the ordering attribute will default all queries on Person to be ordered by last_name, and then first_name. Django will respect this default order for both the admin and when fetching objects.

01:36 The list_display tuple can reference any attribute of the object being listed. It can also reference a method in the admin.ModelAdmin itself.

01:46 Modify PersonAdmin again, as seen on the screen.

02:00 With this code, you add a column to the admin that displays each student’s grade average. The show_average() function is called once for each object displayed on the list.

02:12 The obj parameter is the object for the row that’s being displayed. In this case, you use it to query the corresponding Grade objects for the student, with the response averaged out over Grade.grade.

02:25 You can see the result onscreen. Keep in mind that the average grade should really be calculated in the Person model object. You would likely want the data elsewhere, not just in the admin.

02:36 If you had such a method, you could add it to the list_display attribute. The example here shows what you can do in a ModelAdmin object, but it probably isn’t the best choice for your code. By default, only those columns that are object attributes are sortable. show_average() isn’t an attribute, so it’s not sortable.

02:57 This is because the sorting is performed by an underlying QuerySet, not on the displayed results. There are ways of sorting these columns in some cases, but that’s beyond the scope of this course.

03:08 The title for the column is based on the name of the method. You can alter the title by adding an attribute to the method.

03:29 Django follows good security practice by protecting you from HTML in strings, just in case that’s from user input. To have the display include HTML, you must use format_html(), as seen here.

03:54 Unfortunately, Django hasn’t yet added f-string support for format_html(), so you’re stuck with the .format() syntax. With those changes made, show_average() now has a custom title, "Average Grade", and is formatted to be in bold italics. In the next video, you’ll learn how to link other objects within the admin.

Become a Member to join the conversation.