Modifying a Change List
Modifying a Change List using
.__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.
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
PersonAdmin as follows.
The code you’ve just seen modifies your
Person change list to display the
first_name attributes for each
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
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.
list_display tuple can reference any attribute of the object being listed. It can also reference a method in the
PersonAdmin again, as seen on the screen.
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.
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
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.
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.
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.
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.
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.