liveBook · Manning

Admin.ModelAdmin

As explained in Django in Action

admin.ModelAdmin is Django's base class for configuring how a model is managed in the Django Admin.

admin.ModelAdmin is the Django Admin configuration class for one ORM Model. Define a subclass in admin.py, then register it with the Model. Django uses that pairing to turn stored objects into an administrative web interface rather than making you hand-build routine data-management screens. section 5.1.3 section 5.6

Listing 5.1 The MusicianAdmin class
# RiffMates/bands/admin.py
from django.contrib import admin

from bands.models import Musician  #1

@admin.register(Musician)  #2
class MusicianAdmin(admin.ModelAdmin): #3
    pass  #4

A minimal admin class needs only a Model import, registration, and a subclass of admin.ModelAdmin. An empty class inherits the default admin behavior; the pass is not glamorous, but it is enough to get started. section 5.1.3

Registration can use admin.register(Model) as a class decorator, keeping the association beside the configuration. Once registered, a ModelAdmin provides a listing page plus Add, Change, and Delete pages: CRUD, the pleasantly blunt name for create, read, update, and delete. section 5.1.3 section 5.6

Registering Musician

For RiffMates, MusicianAdmin inherits from admin.ModelAdmin and is registered for Musician. With no extra settings, the listing shows Musician objects; selecting one opens its Change page, while admin links lead to Add and Delete operations. section 5.1.3

The subclass becomes useful when the default listing needs guidance. list_display chooses columns from Model fields, no-argument Model methods, or ModelAdmin methods. Field columns are sortable, while custom callables can return safe HTML such as links to related admin pages. section 5.6

Common listing-page configuration includes:

  • Set search_fields to name the fields searched from the listing page. section 5.6
  • Set list_filter to expose field-based filters; use limited-value fields where showing every value is reasonable. section 5.2.2
  • Extend admin.SimpleListFilter for a tailored filter. Define title and parameter_name, return choices from lookups(), and narrow the page QuerySet in queryset(). section 5.2.2
  • Use query parameters with reverse() lookups when a custom column should link to a related subset of admin objects. section 5.6

Filtering musicians by decade

A DecadeListFilter can offer decade labels such as 1940–1949. Its lookups() method supplies those choices, and queryset() translates the selected decade into birth-date bounds before filtering the listing QuerySet. This avoids a giant, unhelpful filter of individual names or dates. section 5.2.2

Django Admin is intended for restricted users: staff and superusers. Its home page groups registered Models by app, so registration controls which data-management areas appear there. Create a superuser with createsuperuser before expecting administrator access. section 5.6

How Django in Action covers it

How deep it goes
Explains registration and listing customization through columns, search, filters, custom QuerySet filtering, and admin links.
Examples
Demonstrates MusicianAdmin registration and a decade-based birth-date filter for musician listings.
What it assumes
Builds from defining ORM Model classes and locating an app’s admin.py; assumes no prior admin configuration.
What you can do after
Apply it via Django Admin to register models and customize listing columns, search, filters, and related-object links.
How it connects
Connects ModelAdmin with Django Admin, Model, QuerySet, SimpleListFilter, reverse(), and Model Meta.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.