Version 17 (modified by anonymous, 12 years ago) (diff)


The newforms-admin branch

This branch aims to integrate Django's admin site with the newforms library. Along the way, we're taking the opportunity to add extra customization hooks to the admin site.

How to get the branch

svn co

See our branch policy for full information on how to use a branch.


The main goals of this branch are:

  • Change the admin site to use newforms instead of automatic manipulators.
  • Enable developers to declare custom widgets for particular fields in a model.
  • Enable developers to declare custom admin-only validation for a model (i.e., validation logic that is applied only in the admin site, nowhere else).
  • Give developers extra hooks into the admin-site functionality. (Yes, this is a broad goal. More examples are forthcoming.)
  • Remove the admin declarations (class Admin) from within models, thereby helping decouple the admin system from models.
  • Integrate some ideas from #2248: Remove core=True, specify inline models in the model itself rather than in the related model, specify which fields should be displayed inline.

The django.contrib.formtools.preview.FormPreview application (not yet documented, but fully implemented) elegantly allows for fine-grained customization of the application by subclassing. We're taking a similar approach for the new version of the Django admin site: Write a class that subclasses a base class called ModelAdmin and specify whichever customizations you need to make -- from the current basic admin options such as list_display and search_fields to full-on Python hooks, such as defining arbitrary Python code to run before or after a model object is saved via the admin.


A lot has changed in this branch. Let's start the syntax for URLconfs:

# OLD:
from django.conf.urls.defaults import *

urlpatterns = patterns('',
    (r'^admin/', include('django.contrib.admin.urls')),

# NEW:
from django.conf.urls.defaults import *
from django.contrib import admin

urlpatterns = patterns('',

Note that, in this above URLconf example, we're dealing with the object This is an instance of django.contrib.admin.AdminSite, which is a class that lets you specify admin-site functionality. The object is a default AdminSite instance that is created for you automatically, but you can also create other instances as you see fit.

Previously, there was one "global" version of the admin site, which used all models that contained a class Admin. This new scheme allows for much more fine-grained control over your admin sites, allowing you to have multiple admin sites in the same Django instance.

In this example, we create two AdminSite instances, registering different models with both. Assume Book, Author, Musician and Instrument are Django model classes (not instances).

from django.contrib import admin

site1 = admin.AdminSite()

site2 = admin.AdminSite()

# URLconf

from django.conf.urls.defaults import *

urlpatterns = patterns('',
    ('^book_admin/(.*)', site1),
    ('^music_admin/(.*)', site2),

With this example, if you go to /book_admin/, you'll get a Django admin site for the Book and Author models. If you go to /music_admin/, you'll get a Django admin site for the Musician and Instrument models.

Admin options -- the inner class Admin -- have changed, too. Models no longer use an inner class to declare their admin site options. In fact, all admin functionality has been decoupled from the model syntax! How, then, do we declare admin options? Like this:

# a sample file
from django.db import models

class Author(models.Model):
    first_name = models.CharField(maxlength=30)
    last_name = models.CharField(maxlength=30)

    def __str__(self):
        return '%s %s' % (self.first_name, self.last_name)

class Book(models.Model):
    title = models.CharField(maxlength=100)
    author = models.ForeignKey(Author)

from django.contrib import admin

class BookOptions(admin.ModelAdmin):
    list_display = ('title', 'author')
    ordering = ('title',), BookOptions)

In this example, we register both Author and Book with the AdminSite instance Author doesn't need any custom admin options, so we just call Book, on the other hand, has some custom admin options, so we define a BookOptions class and pass that class as a second argument to

(Yes, in this example, the admin options still live in the file. But there's nothing that requires them to do so. The only requirement is that the register() calls are executed at some point, and putting them in the is an easy way to ensure that. We'll likely come up with a nice convention for specifying admin options, perhaps in a file called

You'll notice the BookOptions class looks a lot like the old-style class Admin. Almost all of the old class Admin options work exactly the same, with one or two exceptions. (For the options that have changed, we've made them much more powerful.) In addition to the classic options such as list_display and ordering, the ModelAdmin class introduces a wealth of extra hooks you can use to customize the admin site for that particular model. For example:

class BookOptions(admin.ModelAdmin):
    list_display = ('title', 'author')
    ordering = ('title',)

    def has_change_permission(self, request, obj):
        John can only edit books by Roald Dahl.
        if request.user.username == 'john':
            return == 'Dahl'
        return super(BookOptions, self).has_change_permission(request, obj)

Look at the class ModelAdmin in the file django/contrib/admin/ to see all of the methods you can override. This is exciting stuff.

To-do list

(Updated April 6, 2007)

  • Implement "edit inline" functionality.

Backwards-incompatible changes

This is a (currently incomplete) list of backwards-incompatible changes made in this branch.

Changed Admin.manager option to more flexible hook

As of [4342], the manager option to class Admin no longer exists. This option was undocumented, but we're mentioning the change here in case you used it. In favor of this option, class Admin may now define one of these methods:

  • queryset()
  • queryset_add()
  • queryset_change()

These give you much more flexibility.

Note that this change was made to the NewformsAdminBranch. (We initially called the new method change_list_queryset, but this was changed in [4584] to be more flexible.) The change will not be made to trunk until that branch is merged to trunk.

Changed prepopulate_from to be defined in the Admin class, not database field classes

As of [4446], the prepopulate_from option to database fields no longer exists. It's been discontinued in favor of the new prepopulated_fields option on class Admin. The new prepopulated_fields option, if given, should be a dictionary mapping field names to lists/tuples of field names. Here's an example comparing old syntax and new syntax:

# OLD:
class MyModel(models.Model):
    first_name = models.CharField(maxlength=30)
    last_name = models.CharField(maxlength=30)
    slug = models.CharField(maxlength=60, prepopulate_from=('first_name', 'last_name'))

    class Admin:

# NEW:
class MyModel(models.Model):
    first_name = models.CharField(maxlength=30)
    last_name = models.CharField(maxlength=30)
    slug = models.CharField(maxlength=60)

    class Admin:
        prepopulated_fields = {'slug': ('first_name', 'last_name')}

Note that this change was made to the NewformsAdminBranch. The change will not be made to trunk until that branch is merged to trunk.

Moved admin doc views into django.contrib.admindocs

As of [4585], the documentation views for the Django admin site were moved into a new package, django.contrib.admindocs.

The admin docs, which aren't documented very well, were located at docs/ in the admin site. They're also linked-to by the "Documentation" link in the upper right of default admin templates.

Because we've moved the doc views, you now have to activate admin docs explicitly. Do this by adding the following line to your URLconf:

(r'^admin/doc/', include('django.contrib.admindocs.urls')),

Note that this change has been made to the NewformsAdminBranch. The change will not be made to trunk until that branch is merged to trunk.

Back to Top