Source

django-selectable / docs / admin.rst

Admin Integration

Overview

Django-Selectables will work in the admin. To get started on integrated the fields and widgets in the admin make sure you are familiar with the Django documentation on the ModelAdmin.form and ModelForms particularly on overriding the default widgets. As you will see integrating django-selectable in the adminis the same as working with regular forms.

Including jQuery & jQuery UI

As noted :ref:`in the quick start guide <start-include-jquery>`, the jQuery and jQuery UI libraries are not included in the distribution but must be included in your templates. For the Django admin that means overriding admin/base_site.html. You can include this media in the block name extrahead which is defined in admin/base.html.

See the Django documentation on overriding admin templates. See the example project for the full template example.

Basic Example

In our sample project we have a Farm model with a foreign key to auth.User and a many to many relation to our Fruit model.

In admin.py we will define the form and associate it with the FarmAdmin.

You'll note this form also for new users to be created and associated with the farm if no user is found matching the given name. To make use of this feature we need to add owner to the exclude so that it will pass model validation. Unfortunately that means we must set the owner manual in the save and in the initial data because the ModelForm will no longer do this for you. Since fruit does not allow new items you'll see these steps are not necessary.

The django-selectable widgets are compatitible with the add anther popup in the admin. It's that little green plus sign that appears next to ForeignKey or ManyToManyField items. This makes django-selectable a user friendly replacement for the ModelAdmin.raw_id_fields when the default select box grows too long.

Inline Example

With our Farm model we can also associate the UserAdmin with a Farm by making use of the InlineModelAdmin. We can even make use of the same FarmAdminForm.

The auto-complete functions will be bound as new forms are added dynamically.

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.