How to get the branch

Goals

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.

Status

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

Note that, in this above URLconf example, we're dealing with the object django.contrib.admin.site. This is an instance of django.contrib.admin.AdminSite, which is a class that lets you specify admin-site functionality. The object django.contrib.admin.site 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).

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:

In this example, we register both Author and Book with the AdminSite instance django.contrib.admin.site. Author doesn't need any custom admin options, so we just call admin.site.register(Author). 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 admin.site.register().

In this example, the admin options still live in the models.py 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 models.py is an easy way to ensure that. We'll likely come up with a nice convention for this.
A proposal convention: Specifying all admin options in a file called admin.py, and import it in the __init.py file of your application module to do the registering during the initialization.

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:

Look at the class ModelAdmin in the file [source:/django/branches/newforms-admin/django/contrib/admin/options.py django/contrib/admin/options.py] to see all of the methods you can override. This is exciting stuff.

To-do list

(Updated April 6, 2007)

Implement "edit inline" functionality.

site.register does not handle kwargs for Model Admin.

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.

(We initially called the new method change_list_queryset, but this was changed in [4584] to be more flexible.)

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. This change was made in an effort to remove admin-specific options from the model itself. Here's an example comparing old syntax and new syntax:

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')),

Renamed 'fields' to 'fieldsets'

'Fields' is used to order and group fields in the change form layout.
It is still available in the new admin, but it accepts only a list of fields.
In case one uses fieldsets to organize the fields, one needs to use 'fieldsets' instead.