Performance: List display configuration
This affects how quickly the admin page loads and renders lists of database records.
class MyModelAdmin(admin.ModelAdmin): list_display = ['id', 'name']
class MyModelAdmin(admin.ModelAdmin): list_display = ['id', 'name', 'related_model', 'computed_property', 'long_text_field']
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Minimal fields in list_display | Few DOM nodes | 1 reflow | Low paint cost | [OK] Good |
| Many fields including related/computed | Many DOM nodes | Multiple reflows | High paint cost | [X] Bad |
list_display attribute control?list_displaylist_display attribute in Django admin defines which model fields or methods are shown as columns in the list view of the admin interface.ordering control order, fields control form fields, and database table names are unrelated to list_display.list_display in a Django admin class?list_display attribute expects a list or tuple of strings naming fields or methods. Both list and tuple are valid, but list is most common.class ProductAdmin(admin.ModelAdmin):
list_display = ['name', 'price', 'discounted_price']
def discounted_price(self, obj):
return obj.price * 0.9list_display includes 'discounted_price', which is a method defined in the admin class. Django calls this method for each object to show the computed value.class OrderAdmin(admin.ModelAdmin):
list_display = ['id', 'total_price', 'calculate_tax']
def calculate_tax(self):
return self.total_price * 0.1list_display must accept the model instance as the first parameter, usually named obj. Here, calculate_tax has no parameters, so it will cause an error.list_display?self and obj and returns the desired string.list_display tells Django to call it for each row and show the result.def in_stock(self, obj): return 'Yes' if obj.stock > 0 else 'No' and add 'in_stock' to list_display -> Option APass the test and this pattern moves unknown