Django Admin Site

Activating the Admin Interface

  • Changes to the settings files:
    • Add django.contrib.admin to the INSTALLED_APPS setting.
    • INSTALLED_APPS should contain django.contrib.auth, django.contrib.contenttypes and django.contrib.sessions.
    • MIDDLEWARE_CLASSES should contain django.middleware.common.CommonMiddleware, django.contrib.sessions.middleware.SessionMiddleware and django.contrib.auth.middleware.AuthenticationMiddleware.
  • Run python syncdb. This will install the extra database tables that the admin interface uses.
  • Add the admin site to your URLconf. It should look like this:
# Include these import statements...
from django.contrib import admin
# And include this URLpattern...
urlpatterns = patterns('',
    # ...
    (r'^admin/', include(,
    # ...

Adding Models to the Admin Site

By defaults all new models are not shown in the admin page. To add them you have to create a file called and type:

from django.contrib import admin
from mysite.books.models import Publisher, Author, Book

Custom ModelAdmin classes

The Django admin site offers options that let you customize how the admin site works for a particular model. Such options are in ModelAdmin classes. These are classes that contain configuration for a specific model in a specific admin site instance. There are subclasesses of django.contrib.admin.ModelAdmin and hold custom configuration for a specific admin model.

To display the first and last name in the list of authors and to add a simple search bar you have to do the following:

from django.contrib import admin
from djangobook.books.models import Publisher, Author, Book
class AuthorAdmin(admin.ModelAdmin):
    list_display = ('first_name', 'last_name', 'email')
    search_fields = ('first_name', 'last_name'), AuthorAdmin)

More options for modeladmin

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.