.. _ref-contrib-admin: ===================== 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 five steps in activating the Django admin site: 1. Add ``django.contrib.admin`` to your ``INSTALLED_APPS`` setting. 2. Determine which of your application's models should be editable in the admin interface. 3. For each of those models, optionally create a ``ModelAdmin`` class that encapsulates the customized admin functionality and options for that particular model. 4. Instantiate an ``AdminSite`` and tell it about each of your models and ``ModelAdmin`` classes. 5. 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 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' ``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' ``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`_. ``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:: _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'), } * ``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. ``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. .. 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. ``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. ``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 () for fields that are ``ForeignKey``. Sometimes you don't want to incur the overhead of having to select all the related instances to display in the drop-down. ``raw_id_fields`` is a list of fields you would like to change into a ``Input`` widget for either a ``ForeignKey`` or ``ManyToManyField``:: class BookInline(admin.TabularInline): model = Book raw_id_fields = ("pages",) ``template`` ~~~~~~~~~~~~ The template used to render the inline on the page. ``verbose_name`` ~~~~~~~~~~~~~~~~ An override to the ``verbose_name`` found in the model's inner ``Meta`` class. ``verbose_name_plural`` ~~~~~~~~~~~~~~~~~~~~~~~ An override to the ``verbose_name_plural`` found in the model's inner ``Meta`` class. Working with a model with two or more foreign keys to the same parent model --------------------------------------------------------------------------- It is sometimes possible to have more than one foreign key to the same model. Take this model for instance:: class Friendship(models.Model): to_person = models.ForeignKey(Person, related_name="friends") from_person = models.ForeignKey(Person, related_name="from_friends") If you wanted to display an inline on the ``Person`` admin add/change pages you need to explicitly define the foreign key since it is unable to do so automatically:: class FriendshipInline(admin.TabularInline): model = Friendship fk_name = "to_person" class PersonAdmin(admin.ModelAdmin): inlines = [ FriendshipInline, ] Working with Many-to-Many Intermediary Models ---------------------------------------------- By default, admin widgets for many-to-many relations will be displayed inline on whichever model contains the actual reference to the ``ManyToManyField``. However, when you specify an intermediary model using the ``through`` argument to a ``ManyToManyField``, the admin will not display a widget by default. This is because each instance of that intermediary model requires more information than could be displayed in a single widget, and the layout required for multiple widgets will vary depending on the intermediate model. However, we still want to be able to edit that information inline. Fortunately, this is easy to do with inline admin models. Suppose we have the following models:: class Person(models.Model): name = models.CharField(max_length=128) class Group(models.Model): name = models.CharField(max_length=128) members = models.ManyToManyField(Person, through='Membership') class Membership(models.Model): person = models.ForeignKey(Person) group = models.ForeignKey(Group) date_joined = models.DateField() invite_reason = models.CharField(max_length=64) The first step in displaying this intermediate model in the admin is to define an inline class for the ``Membership`` model:: class MembershipInline(admin.TabularInline): model = Membership extra = 1 This simple example uses the default ``InlineModelAdmin`` values for the ``Membership`` model, and limits the extra add forms to one. This could be customized using any of the options available to ``InlineModelAdmin`` classes. Now create admin views for the ``Person`` and ``Group`` models:: class PersonAdmin(admin.ModelAdmin): inlines = (MembershipInline,) class GroupAdmin(admin.ModelAdmin): inlines = (MembershipInline,) Finally, register your ``Person`` and ``Group`` models with the admin site:: admin.site.register(Person, PersonAdmin) admin.site.register(Group, GroupAdmin) Now your admin site is set up to edit ``Membership`` objects inline from either the ``Person`` or the ``Group`` detail pages. Using generic relations as an inline ------------------------------------ It is possible to use an inline with generically related objects. Let's say you have the following models:: class Image(models.Model): image = models.ImageField(upload_to="images") content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() content_object = generic.GenericForeignKey("content_type", "object_id") class Product(models.Model): name = models.CharField(max_length=100) If you want to allow editing and creating ``Image`` instance on the ``Product`` add/change views you can simply use ``GenericInlineModelAdmin`` provided by ``django.contrib.contenttypes.generic``. In your ``admin.py`` for this example app:: from django.contrib import admin from django.contrib.contenttypes import generic from myproject.myapp.models import Image, Product class ImageInline(generic.GenericTabularInline): model = Image class ProductAdmin(admin.ModelAdmin): inlines = [ ImageInline, ] admin.site.register(Product, ProductAdmin) ``django.contrib.contenttypes.generic`` provides both a ``GenericTabularInline`` and ``GenericStackedInline`` and behave just like any other inline. See the :ref:`contenttypes documentation ` for more specific information. Overriding Admin Templates ========================== It is relatively easy to override many of the templates which the admin module uses to generate the various pages of an admin site. You can even override a few of these templates for a specific app, or a specific model. Set up your projects admin template directories ----------------------------------------------- The admin template files are located in the ``contrib/admin/templates/admin`` directory. In order to override one or more of them, first create an ``admin`` directory in your project's ``templates`` directory. This can be any of the directories you specified in ``TEMPLATE_DIRS``. Within this ``admin`` directory, create sub-directories named after your app. Within these app subdirectories create sub-directories named after your models. Note, that the admin app will lowercase the model name when looking for the directory, so make sure you name the directory in all lowercase if you are going to run your app on a case-sensitive filesystem. To override an admin template for a specific app, copy and edit the template from the ``django/contrib/admin/templates/admin`` directory, and save it to one of the directories you just created. For example, if we wanted to add a tool to the change list view for all the models in an app named ``my_app``, we would copy ``contrib/admin/templates/admin/change_list.html`` to the ``templates/admin/my_app/`` directory of our project, and make any necessary changes. If we wanted to add a tool to the change list view for only a specific model named 'Page', we would copy that same file to the ``templates/admin/my_app/page`` directory of our project. Overriding vs. replacing an admin template ------------------------------------------ Because of the modular design of the admin templates, it is usually neither necessary nor advisable to replace an entire template. It is almost always better to override only the section of the template which you need to change. To continue the example above, we want to add a new link next to the ``History`` tool for the ``Page`` model. After looking at ``change_form.html`` we determine that we only need to override the ``object-tools`` block. Therefore here is our new ``change_form.html`` : .. code-block:: html+django {% extends "admin/change_form.html" %} {% load i18n %} {% block object-tools %} {% if change %}{% if not is_popup %} {% endif %}{% endif %} {% endblock %} And that's it! If we placed this file in the ``templates/admin/my_app`` directory, our link would appear on every model's change form. Templates which may be overridden per app or model -------------------------------------------------- Not every template in ``contrib\admin\templates\admin`` may be overridden per app or per model. The following can: * ``change_form.html`` * ``change_list.html`` * ``delete_confirmation.html`` * ``object_history.html`` For those templates that cannot be overridden in this way, you may still override them for your entire project. Just place the new version in your ``templates/admin`` directory. This is particularly useful to create custom 404 and 500 pages. .. note:: Some of the admin templates, such as ``change_list_request.html`` are used to render custom inclusion tags. These may be overridden, but in such cases you are probably better off creating your own version of the tag in question and giving it a different name. That way you can use it selectively. Root and login templates ------------------------ If you wish to change the index or login templates, you are better off creating your own ``AdminSite`` instance (see below), and changing the ``index_template`` or ``login_template`` properties. ``AdminSite`` objects ===================== A Django administrative site is represented by an instance of ``django.contrib.admin.sites.AdminSite``; by default, an instance of this class is created as ``django.contrib.admin.site`` and you can register your models and ``ModelAdmin`` instances with it. If you'd like to set up your own administrative site with custom behavior, however, you're free to subclass ``AdminSite`` and override or add anything you like. Then, simply create an instance of your ``AdminSite`` subclass (the same way you'd instantiate any other Python class), and register your models and ``ModelAdmin`` subclasses with it instead of using the default. Hooking ``AdminSite`` instances into your URLconf ------------------------------------------------- The last step in setting up the Django admin is to hook your ``AdminSite`` instance into your URLconf. Do this by pointing a given URL at the ``AdminSite.root`` method. In this example, we register the default ``AdminSite`` instance ``django.contrib.admin.site`` at the URL ``/admin/`` :: # urls.py from django.conf.urls.defaults import * from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', ('^admin/(.*)', admin.site.root), ) Above we used ``admin.autodiscover()`` to automatically load the ``INSTALLED_APPS`` admin.py modules. In this example, we register the ``AdminSite`` instance ``myproject.admin.admin_site`` at the URL ``/myadmin/`` :: # urls.py from django.conf.urls.defaults import * from myproject.admin import admin_site urlpatterns = patterns('', ('^myadmin/(.*)', admin_site.root), ) There is really no need to use autodiscover when using your own ``AdminSite`` instance since you will likely be importing all the per-app admin.py modules in your ``myproject.admin`` module. Note that the regular expression in the URLpattern *must* group everything in the URL that comes after the URL root -- hence the ``(.*)`` in these examples. Multiple admin sites in the same URLconf ---------------------------------------- It's easy to create multiple instances of the admin site on the same Django-powered Web site. Just create multiple instances of ``AdminSite`` and root each one at a different URL. In this example, the URLs ``/basic-admin/`` and ``/advanced-admin/`` feature separate versions of the admin site -- using the ``AdminSite`` instances ``myproject.admin.basic_site`` and ``myproject.admin.advanced_site``, respectively:: # urls.py from django.conf.urls.defaults import * from myproject.admin import basic_site, advanced_site urlpatterns = patterns('', ('^basic-admin/(.*)', basic_site.root), ('^advanced-admin/(.*)', advanced_site.root), )