Customizing the Venue Change List
The default change list view is not very user-friendly—it just lists the venue names in no particular order. To make the list display more useful, we will make some customizations:
- Show the venue names in alphabetical order to make browsing easier.
- Add the venue address and phone number to the listing to make it easier to access key contact information; and
- Add name and address search capabilities, so once the venue list gets larger, it’s easier to find a particular venue.
In Django, each model is represented in the admin interface by the ModelAdmin
class. To customize how a model displays in the admin, you can set several options in a custom ModelAdmin
subclass. Open your admin.py
file and add a custom VenueAdmin
class (changes in bold):
# \myclub_root\events\admin.py
1 from django.contrib import admin
2 from .models import Venue, MyClubUser, Event
3
4
5 @admin.register(Venue)
6 class VenueAdmin(admin.ModelAdmin):
7 list_display = ('name', 'address', 'phone')
8 ordering = ('name',)
9 search_fields = ('name', 'address')
10
11
12 # admin.site.register(Venue)
13 admin.site.register(MyClubUser)
14 admin.site.register(Event)
Let’s have a closer look at this new class:
- Line 5. We’re using the
register
decorator to register the new class with the admin. Theregister
decorator is functionally equivalent to theregister
method. I am using the decorator here because most books and online tutorials use theregister
method and I wanted to give you an example of an alternative way of registering your customModelAdmin
subclasses. If you wanted to use the register method, you would delete line 5 and replace line 12 withadmin.site.register(Venue, VenueAdmin)
. - Line 6 is your
VenueAdmin
class declaration, which subclasses theadmin.ModelAdmin
class. - Line 7 sets the
list_display
option to show the venue name, address and phone number in the venue change list. - Line 8 sets the default sort order to the venue name. As
ordering
is a 1-tuple (singleton), don’t forget the comma on the end! TIP: If you wanted the default sort to be in reverse order, you can useordering = ('-<fieldname>',)
. In this example, if you wanted to sort in reverse order by venue name it would beordering = ('-name',)
. - Line 9 sets the default search fields to the venue name and venue address.
- Line 12. As we’re using the
register
decorator, line 12 is not needed, so I have commented it out.
If you refresh the admin in your browser, the venue change list should now look like Figure 7-7.
Figure 7-7: The venue admin now has search capability, the venues are listed in alphabetical order, and the venue address and phone number columns have been added to the change list.
Looking at your customized venue change list, you will note that:
- The venues are listed in alphabetical order.
- Each venue’s address and phone number are listed, along with the venue name; and
- A search bar has been added to the change list page. Django will perform a case-insensitive search of the venue name and address fields for whatever term you enter into the search box. To show all records again, search for an empty string.