ModelAdmin
Options
ModelAdmin
allows us to create admin interface quickly and much flexibility (opens in a new tab).
list_display
list_display
(opens in a new tab) determines which fields of models to show on the admin
panel.
Let's say we have User
model defined as below:
Then it will display as below.
Or you can also include customized fields by using custom methods based on the fields information.
Below example shows how to print the full name of user combining with his/her first_name
and last_name
and make them all uppercase.
list_filter
list_filter
(opens in a new tab) reorgnizes the information displayed on list_display
by certain condition that admin user chose.
For instance, we can rearrange the user data table based on is_active
and is_admin
as below.
search_fields
search_fields
(opens in a new tab) allows us to search fields on list_display
with certain keywords.
Above example shows that admin user can search users by their email
field. Of course, we can include multiple fields if we want to.
By default, search_fields
provides icontains
(opens in a new tab) feature that searches all results containing certain keywords. However, we can also use startswith
(opens in a new tab) feature by using ^
for searching words that starts only with that keyword.
For fields that are connected by ForeignKey
or ManyToManyField
, we can search results as below:
search_fields = ["foreign_key__related_fieldname"]