Admin Integration


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-Selectables in the admin is the same as working with regular forms.

Including jQuery & jQuery UI

As noted in the quick start guide, 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.

{% block extrahead %}
<link rel="stylesheet" href="" type="text/css" media="screen">
<script type="text/javascript" src=""></script>
<script type="text/javascript" src=""></script>
{% endblock %}

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.

class Farm(models.Model):
    name = models.CharField(max_length=200)
    owner = models.ForeignKey('auth.User', related_name='farms')
    fruit = models.ManyToManyField(Fruit)

    def __unicode__(self):
        return u"%s's Farm: %s" % (self.owner.username,

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

class FarmAdminForm(forms.ModelForm):
    owner = selectable.AutoComboboxSelectField(lookup_class=OwnerLookup, allow_new=True)

    class Meta(object):
        model = Farm
        widgets = {
            'fruit': selectable.AutoCompleteSelectMultipleWidget(lookup_class=FruitLookup),
        exclude = ('owner', )

    def __init__(self, *args, **kwargs):
        super(FarmAdminForm, self).__init__(*args, **kwargs)
        if self.instance and and self.instance.owner:
            self.initial['owner'] = self.instance.owner

    def save(self, *args, **kwargs):
        owner = self.cleaned_data['owner']
        if owner and not
            owner = User.objects.create_user(username=owner.username, email='')
        self.instance.owner = owner
        return super(FarmAdminForm, self).save(*args, **kwargs)
class FarmAdmin(admin.ModelAdmin):
    form = FarmAdminForm

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.

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.

class FarmInline(admin.TabularInline):
    model = Farm
    form = FarmAdminForm
class NewUserAdmin(UserAdmin):
    inlines = [

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

Project Versions

Table Of Contents

Previous topic

Additional Parameters

Next topic


This Page