
PYTHON — Modifying Change List in Python
Social media is not about the exploitation of technology but service to community. — Simon Mainwaring

PYTHON — File System Summary in Python
Modifying a Change List in Django Admin
In this tutorial, you will learn how to modify a change list in Django Admin using the list_display attribute. The list_display attribute of an admin.ModelAdmin object specifies what columns are shown in the change list. It allows you to customize change list pages in various ways. Let's dive into examples to understand how to use list_display in Django Admin.
Implementing .__str__() Method The .__str__() method is a quick way to change the representation of a Person object from a meaningless string to understandable data. This representation will also show up in drop-downs and multi-selects. You can customize the change list pages by modifying an object’s string representation.
# Implementing `.__str__()` method
class Person(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
def __str__(self):
return f'{self.last_name}, {self.first_name}'Using list_display Attribute The following example modifies the Person change list to display the last_name and first_name attributes for each Person object.
# Modifying `PersonAdmin` to include `last_name` and `first_name` in the change list
from django.contrib import admin
from .models import Person
class PersonAdmin(admin.ModelAdmin):
list_display = ('last_name', 'first_name')
admin.site.register(Person, PersonAdmin)Adding Sorting with ordering Attribute 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.
# Adding the `ordering` attribute to default the sorting order
class PersonAdmin(admin.ModelAdmin):
list_display = ('last_name', 'first_name')
ordering = ('last_name', 'first_name')Customizing Display with a Method You can add a column to the admin that displays each student’s grade average by using a method in the admin.ModelAdmin itself.
# Modifying `PersonAdmin` to include a column displaying student’s grade average
class PersonAdmin(admin.ModelAdmin):
list_display = ('last_name', 'first_name', 'show_average')
def show_average(self, obj):
# Calculate average grade
return obj.calculate_average_grade()Customizing Column Title and Format You can alter the title for the column by adding an attribute to the method and customize the display using format_html().
# Customizing column title and format using `format_html()`
from django.utils.html import format_html
class PersonAdmin(admin.ModelAdmin):
list_display = ('last_name', 'first_name', 'show_average')
def show_average(self, obj):
# Calculate average grade
return obj.calculate_average_grade()
show_average.short_description = 'Average Grade'
def show_average(self, obj):
# Calculate average grade
return format_html('<b><i>{}</i></b>', obj.calculate_average_grade())Conclusion In this tutorial, you learned how to customize change list pages in Django Admin using the list_display attribute. You can modify the display of columns, add sorting, and customize the display using methods and HTML formatting. The list_display attribute provides you with the flexibility to tailor the change list according to your requirements.
I hope this tutorial helps you understand how to modify change lists in Django Admin effectively.






