avatarLaxfed Paulacy

Summary

The provided content is a tutorial on customizing change list pages in Django Admin using the list_display attribute to modify column display, add sorting, and enhance the presentation with HTML formatting.

Abstract

The article titled "PYTHON — Modifying Change List in Python" is a comprehensive guide aimed at Python developers working with Django Admin. It explains how to utilize the list_display attribute within an admin.ModelAdmin object to specify which columns appear in the change list. The tutorial covers practical examples, such as implementing the __str__() method to improve the representation of objects, using the ordering attribute to set a default sorting order, and adding custom columns with methods like show_average to display calculated data. It also demonstrates how to customize column titles and formats using format_html() for richer visual presentation. The conclusion emphasizes the flexibility provided by list_display for tailoring change list pages to specific needs, ensuring that developers can effectively modify change lists in Django Admin.

Opinions

  • The author suggests that social media should be viewed as a service to the community rather than just a technological exploit, quoting Simon Mainwaring.
  • The tutorial conveys the importance of customizing the Django Admin change list pages for better usability and data representation.
  • The use of the __str__() method is recommended for a more meaningful representation of objects within the Django Admin interface.
  • The ordering attribute is presented as a valuable tool for maintaining a consistent sorting order across the admin interface and when fetching objects.
  • The article highlights the benefits of adding custom methods to the admin.ModelAdmin to include additional information, such as a student's grade average, in the change list.
  • The author emphasizes the flexibility of list_display in allowing developers to create a more intuitive and informative admin interface tailored to their specific requirements.

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.

PYTHON — Copy Move Rename in Python

ChatGPT
Change
List
Python
Modifying
Recommended from ReadMedium