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:

  1. Show the venue names in alphabetical order to make browsing easier.
  2. Add the venue address and phone number to the listing to make it easier to access key contact information; and
  3. 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):

  1. # \myclub_root\events\admin.py
  2. 1 from django.contrib import admin
  3. 2 from .models import Venue, MyClubUser, Event
  4. 3
  5. 4
  6. 5 @admin.register(Venue)
  7. 6 class VenueAdmin(admin.ModelAdmin):
  8. 7 list_display = ('name', 'address', 'phone')
  9. 8 ordering = ('name',)
  10. 9 search_fields = ('name', 'address')
  11. 10
  12. 11
  13. 12 # admin.site.register(Venue)
  14. 13 admin.site.register(MyClubUser)
  15. 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. The register decorator is functionally equivalent to the register method. I am using the decorator here because most books and online tutorials use the register method and I wanted to give you an example of an alternative way of registering your custom ModelAdmin subclasses. If you wanted to use the register method, you would delete line 5 and replace line 12 with admin.site.register(Venue, VenueAdmin).
  • Line 6 is your VenueAdmin class declaration, which subclasses the admin.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 use ordering = ('-<fieldname>',). In this example, if you wanted to sort in reverse order by venue name it would be ordering = ('-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.

Customizing the Venue Change List - 图1

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:

  1. The venues are listed in alphabetical order.
  2. Each venue’s address and phone number are listed, along with the venue name; and
  3. 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.