===================== The Django admin site ===================== .. module:: django.contrib.admin :synopsis: Django's 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 six steps in activating the Django admin site: 1. Add ``'django.contrib.admin'`` to your :setting:`INSTALLED_APPS` setting. 2. Admin has two dependencies - ``django.contrib.auth`` and ``django.contrib.contenttypes``. If these applications are not in your :setting:`INSTALLED_APPS` list, add them. 3. Determine which of your application's models should be editable in the admin interface. 4. For each of those models, optionally create a ``ModelAdmin`` class that encapsulates the customized admin functionality and options for that particular model. 5. Instantiate an ``AdminSite`` and tell it about each of your models and ``ModelAdmin`` classes. 6. Hook the ``AdminSite`` instance into your URLconf. Other topics ------------ .. toctree:: :maxdepth: 1 actions admindocs .. seealso:: For information about serving the media files (images, JavaScript, and CSS) associated with the admin in production, see :ref:`serving-media-files`. ``ModelAdmin`` objects ====================== .. class:: ModelAdmin 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 of the ``ModelAdmin``:: from django.contrib import admin from myproject.myapp.models import Author class AuthorAdmin(admin.ModelAdmin): pass admin.site.register(Author, AuthorAdmin) .. admonition:: Do you need a ``ModelAdmin`` object at all? In the preceding example, the ``ModelAdmin`` class doesn't define any custom values (yet). As a result, the default admin interface will be provided. If you are happy with the default admin interface, you don't need to define a ``ModelAdmin`` object at all -- you can register the model class without providing a ``ModelAdmin`` description. The preceding example could be simplified to:: from django.contrib import admin from myproject.myapp.models import Author admin.site.register(Author) ``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' .. attribute:: ModelAdmin.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' .. versionadded:: 1.3 This will intelligently populate itself based on available data, e.g. if all the dates are in one month, it'll show the day-level drill-down only. .. attribute:: ModelAdmin.form By default a ``ModelForm`` is dynamically created for your model. It is used to create the form presented on both the add/change pages. You can easily provide your own ``ModelForm`` to override any default form behavior on the add/change pages. For an example see the section `Adding custom validation to the admin`_. .. attribute:: ModelAdmin.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 :class:`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:: _images/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'), } .. versionadded:: 1.2 ``fields`` can contain values defined in :attr:`ModelAdmin.readonly_fields` to be displayed as read-only. * ``classes`` A list containing extra CSS classes to apply to the fieldset. 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. Note that this value is *not* HTML-escaped when it's displayed in the admin interface. This lets you include HTML if you so desire. Alternatively you can use plain text and ``django.utils.html.escape()`` to escape any HTML special characters. .. attribute:: ModelAdmin.fields Use this option as an alternative to ``fieldsets`` if the layout does not matter and if you want to only show a subset of the available fields in the form. For example, you could define a simpler version of the admin form for the ``django.contrib.flatpages.FlatPage`` model as follows:: class FlatPageAdmin(admin.ModelAdmin): fields = ('url', 'title', 'content') In the above example, only the fields 'url', 'title' and 'content' will be displayed, sequentially, in the form. .. versionadded:: 1.2 ``fields`` can contain values defined in :attr:`ModelAdmin.readonly_fields` to be displayed as read-only. .. admonition:: Note This ``fields`` option should not be confused with the ``fields`` dictionary key that is within the ``fieldsets`` option, as described in the previous section. .. attribute:: ModelAdmin.exclude This attribute, if given, should be a list of field names to exclude from the form. For example, let's consider the following model:: class Author(models.Model): name = models.CharField(max_length=100) title = models.CharField(max_length=3) birth_date = models.DateField(blank=True, null=True) If you want a form for the ``Author`` model that includes only the ``name`` and ``title`` fields, you would specify ``fields`` or ``exclude`` like this:: class AuthorAdmin(admin.ModelAdmin): fields = ('name', 'title') class AuthorAdmin(admin.ModelAdmin): exclude = ('birth_date',) Since the Author model only has three fields, ``name``, ``title``, and ``birth_date``, the forms resulting from the above declarations will contain exactly the same fields. .. attribute:: ModelAdmin.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. .. attribute:: ModelAdmin.raw_id_fields By default, Django's admin uses a select-box interface (