Configuring List Display in Django Admin

django admin set list display
10 November 2024

When working with Django, one of the key features of its admin section is that it allows you to manage your data models easily. One of the capabilities that can significantly save time and increase efficiency is the ability to configure the List Display section.

List Display gives you the option to specify which instances of your models are displayed on the list page. Instead of only seeing the titles of items, you can also present additional details in various columns next to each item.

To achieve this, you must customize your Admin class. You can do this by adding a feature called list_display in your Admin class, which allows you to easily control what data is displayed.

This feature includes a list of field names that you want to display in the table. Let's look at a simple example to better understand this feature.

Assume you have a model called Book that includes fields like title, author, publication date, and publisher. Now we want to display this information in the Admin panel.


from django.contrib import admin
from .models import Book

class BookAdmin(admin.ModelAdmin):
list_display = ('title', 'author', 'publication_date', 'publisher')

admin.site.register(Book, BookAdmin)

In the code above, we first imported the model Book from the models file. Then we created a class BookAdmin which inherits from admin.ModelAdmin. In this class, we configured the list_display feature and imported the fields we want to display. Finally, using the admin.site.register method, we added the model Book and the class BookAdmin to the Admin panel.

Each time the Admin page is opened for the Book model, you will see columns showing related information such as title, author, publication date, and publisher. This feature helps you quickly find relevant information related to the models you are managing.

FAQ

?

How can I display more information in the Django Admin panel?

?

Why should we use List Display?

?

Can we define computed fields in List Display?