Performance: Admin customization with ModelAdmin
This affects the admin interface load time and responsiveness by controlling how data is queried and displayed.
class BookAdmin(admin.ModelAdmin): list_display = ('title', 'author_name') list_select_related = ('author',) def author_name(self, obj): return obj.author.name
class BookAdmin(admin.ModelAdmin): list_display = ('title', 'author_name') def author_name(self, obj): return obj.author.name
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Using list_select_related to fetch related data | Minimal extra DOM nodes | Single reflow | Low paint cost | [OK] Good |
| Querying related data per row without optimization | More DOM nodes due to delayed data | Multiple reflows | Higher paint cost | [X] Bad |
| Displaying many fields in list_display | Large DOM tree | Multiple reflows | High paint cost | [X] Bad |
| Limiting fields in list_display | Smaller DOM tree | Single reflow | Low paint cost | [OK] Good |
ModelAdmin class?ModelAdmin class customizes the admin interface for a model, such as which fields show and how data is filtered.ModelAdmin in Django admin?admin.site.register(Model, ModelAdmin).admin.register or admin.site.add do not exist in Django's admin API.ModelAdmin code, what fields will appear in the Django admin list view?class BookAdmin(admin.ModelAdmin):
list_display = ('title', 'author', 'published_date')
admin.site.register(Book, BookAdmin)list_display attribute defines exactly which fields show in the admin list view.ModelAdmin code snippet?class AuthorAdmin(admin.ModelAdmin):
search_fields = 'name'
admin.site.register(Author, AuthorAdmin)search_fields attribute must be a list or tuple of field names, not a single string.admin.ModelAdmin is correct, registration syntax is correct, and search_fields is valid in ModelAdmin.ModelAdmin attribute should you customize, and how?list_filter attribute adds filter sidebars for specified fields in the admin list view.filter_fields and filter_sidebar do not exist; search_fields is for search, not filters.list_filter = ('genre', 'published_year') in the ModelAdmin class -> Option DPass the test and this pattern moves unknown