Customizing how models are displayed
In Django, ModelAdmin is a class used in the Django admin interface to customize how models are displayed and interacted with. It allows you to specify various attributes and methods to control the behavior and appearance of models in the admin interface.
Now, we will take a look at how to customize the administration site.
from django.contrib import admin
from .models import Post
@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
list_display = ['title', 'slug', 'author', 'publish', 'status']
list_filter = ['status', 'created', 'publish', 'author']
search_fields = ['title', 'body']
prepopulated_fields = {'slug': ('title',)}
raw_id_fields = ['author']
date_hierarchy = 'publish'
ordering = ['status', 'publish']
Understanding ModelAdmin
The ModelAdmin class serves as a bridge between Django models and the admin interface. By defining a ModelAdmin class for each model, developers gain fine-grained control over how data is presented and manipulated in the admin interface.
Here are some common attributes used in ModelAdmin:
Note
- list_display: Specifies which fields should be displayed as columns in the list view of the admin interface.
- list_filter: Enables filtering of data based on specified fields, providing users with options to refine the displayed records.
- search_fields: Allows users to search for specific records within the admin interface, typically by specifying relevant field names.
- readonly_fields: Specifies fields that should be read-only in the admin interface, preventing users from modifying them.
- actions: Defines custom actions that can be performed on selected items in the admin interface, such as deleting multiple records at once.
- fieldsets: Organizes fields into sections in the detail view for improved readability and organization.
- list_editable: Specifies which fields can be edited directly from the list view, providing a convenient way to update multiple records at once.
- list_display_links: Specifies which fields should link to the change view from the list view, allowing users to quickly navigate to detailed. information.
- list_per_page: Sets the number of items displayed per page in the list view, allowing developers to control pagination.
- ordering: Defines the default ordering of records in the admin interface, typically based on one or more fields.
- list_display: A tuple or list of model field names that should be displayed as columns in the model's list view in the admin interface.
- list_filter: A tuple or list of field names that should be used as filters in the list view.
- search_fields: A tuple or list of field names that should be searchable in the admin interface.
- list_editable: A tuple or list of field names that can be edited directly from the list view.
- list_display_links: A tuple or list of field names that should link to the change view from the list view.
- list_per_page: An integer specifying the number of items displayed per page in the list view.
- list_select_related: A boolean specifying whether to use select_related() in the queryset for the list view.
- ordering: A tuple or list of field names used to order the queryset in the admin interface.
- fields: A tuple or list of field names to be displayed in the detail view of the model.
- fieldsets: A list of two-tuples, where each two-tuple represents a section of fields in the detail view.
- readonly_fields: A tuple or list of field names that should be read-only in the admin interface.
- date_hierarchy: A string representing the name of a DateField or DateTimeField used to create a date-based drilldown navigation in the admin interface.
- prepopulated_fields: A dictionary specifying fields whose values are automatically set based on other fields.
- actions: A list of custom actions that can be performed on selected items in the admin interface.
- filter_horizontal and filter_vertical: A tuple or list of field names used to specify ManyToManyField fields displayed as horizontal or vertical filter widgets, respectively.
These are just a few of the most commonly used attributes of the ModelAdmin class. Depending on your needs, you may also override methods such as get_queryset(), formfield_for_foreignkey(), get_form() etc., to further customize the behavior of your model in the admin interface.