===================== The Django admin site ===================== One of the most powerful parts of Django is the automatic admin interface. It reads metadata in your model to provide a powerful and production-ready interface that content producers can immediately use to start adding content to the site. In this document, we discuss how to activate, use and customize Django's admin interface. .. admonition:: Note The admin site has been refactored significantly since Django 0.96. This document describes the newest version of the admin site, which allows for much richer customization. If you follow the development of Django itself, you may have heard this described as "newforms-admin." Overview ======== There are four steps in activating the Django admin site: 1. Determine which of your application's models should be editable in the admin interface. 2. For each of those models, optionally create a ``ModelAdmin`` class that encapsulates the customized admin functionality and options for that particular model. 3. Instantiate an ``AdminSite`` and tell it about each of your models and ``ModelAdmin`` classes. 4. Hook the ``AdminSite`` instance into your URLconf. ``ModelAdmin`` objects ====================== The ``ModelAdmin`` class is the representation of a model in the admin interface. These are stored in a file named ``admin.py`` in your application. Let's take a look at a very simple example the ``ModelAdmin``:: from django.contrib import admin from myproject.myapp.models import Author class AuthorAdmin(admin.ModelAdmin): pass admin.site.register(Author, AuthorAdmin) ``ModelAdmin`` Options ---------------------- The ``ModelAdmin`` is very flexible. It has several options for dealing with customizing the interface. All options are defined on the ``ModelAdmin`` subclass:: class AuthorAdmin(admin.ModelAdmin): date_hierarchy = 'pub_date' ``date_hierarchy`` ~~~~~~~~~~~~~~~~~~ Set ``date_hierarchy`` to the name of a ``DateField`` or ``DateTimeField`` in your model, and the change list page will include a date-based drilldown navigation by that field. Example:: date_hierarchy = 'pub_date' ``fieldsets`` ~~~~~~~~~~~~~ Set ``fieldsets`` to control the layout of admin "add" and "change" pages. ``fieldsets`` is a list of two-tuples, in which each two-tuple represents a ``
`` on the admin form page. (A ``
`` is a "section" of the form.) The two-tuples are in the format ``(name, field_options)``, where ``name`` is a string representing the title of the fieldset and ``field_options`` is a dictionary of information about the fieldset, including a list of fields to be displayed in it. A full example, taken from the ``django.contrib.flatpages.FlatPage`` model:: class FlatPageAdmin(admin.ModelAdmin): fieldsets = ( (None, { 'fields': ('url', 'title', 'content', 'sites') }), ('Advanced options', { 'classes': ('collapse',), 'fields': ('enable_comments', 'registration_required', 'template_name') }), ) This results in an admin page that looks like: .. image:: http://media.djangoproject.com/img/doc/flatfiles_admin.png If ``fieldsets`` isn't given, Django will default to displaying each field that isn't an ``AutoField`` and has ``editable=True``, in a single fieldset, in the same order as the fields are defined in the model. The ``field_options`` dictionary can have the following keys: ``fields`` A tuple of field names to display in this fieldset. This key is required. Example:: { 'fields': ('first_name', 'last_name', 'address', 'city', 'state'), } To display multiple fields on the same line, wrap those fields in their own tuple. In this example, the ``first_name`` and ``last_name`` fields will display on the same line:: { 'fields': (('first_name', 'last_name'), 'address', 'city', 'state'), } ``classes`` A string containing extra CSS classes to apply to the fieldset. Example:: { 'classes': 'wide', } Apply multiple classes by separating them with spaces. Example:: { 'classes': 'wide extrapretty', } Two useful classes defined by the default admin-site stylesheet are ``collapse`` and ``wide``. Fieldsets with the ``collapse`` style will be initially collapsed in the admin and replaced with a small "click to expand" link. Fieldsets with the ``wide`` style will be given extra horizontal space. ``description`` A string of optional extra text to be displayed at the top of each fieldset, under the heading of the fieldset. It's used verbatim, so you can use any HTML and you must escape any special HTML characters (such as ampersands) yourself. ``filter_horizontal`` ~~~~~~~~~~~~~~~~~~~~~ Use a nifty unobtrusive Javascript "filter" interface instead of the usability-challenged ``) for fields that are ``ForeignKey`` or have ``choices`` set. If a field is present in ``radio_fields``, Django will use a radio-button interface instead. Assuming ``group`` is a ``ForeignKey`` on the ``Person`` model:: class PersonAdmin(admin.ModelAdmin): radio_fields = {"group": admin.VERTICAL} You have the choice of using ``HORIZONTAL`` or ``VERTICAL`` from the ``django.contrib.admin`` module. Don't include a field in ``radio_fields`` unless it's a ``ForeignKey`` or has ``choices`` set. ``raw_id_fields`` ~~~~~~~~~~~~~~~~~ By default, Django's admin uses a select-box interface (