Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Dynamic select fields for the Django Admin that just works. See trailer ↵
Python JavaScript
Latest commit 2d6325b @runekaagaard #6: Make the user specify a unique name.
If you in your flexselect widget sets a ``unique_name`` property it bypasses the faulty mechanism of magically creating a safe hashed name. Example:

        unique_name = 'my_flexselect_widget' # A name that is allowed to be shown in the HTML.
        ... etc.


About Django Admin FlexSelect

FlexSelect is a small app for the Django Admin that makes it trivial to have foreign keys depend on each other. By depend I mean that choices and additional content of one field updates dynamically when another is changed.

Usage example

See the video at

In the following we will define a Case model with two foreign key fields, a base field client and a trigger field company_contact_person. When we change the client on the Case change view the company_contact_person updates accordingly. Furthermore we will display the customer_contact_persons company and email as additional details.

In "":

from django.db import models as m
from django.core.exceptions import ValidationError

No changes to the models are needed to use flexselect.

class Company(m.Model):
    name = m.CharField(max_length=80)

    def __unicode__(self):

class CompanyContactPerson(m.Model):
    company = m.ForeignKey(Company)
    name = m.CharField(max_length=80)
    email = m.EmailField()

    def __unicode__(self):

class Client(m.Model):
    company = m.ForeignKey(Company)
    name = m.CharField(max_length=80)

    def __unicode__(self):

class Case(m.Model):
    client = m.ForeignKey(Client)
    company_contact_person = m.ForeignKey(CompanyContactPerson)

    def clean(self):
        Makes sure the company for client is the same as the company for the 
        customer contact person. Note that you need to check for `None` too
        if the fields are not required.
        if not ==
            raise ValidationError('The clients and the contacts company does'
                                  ' not match.')

    def __unicode__(self):
        return u'Case: %d' %

In "":

from django.contrib import admin
from flexselect import FlexSelectWidget
from test_flexselect.tests.models import (Company, Case, Client, 

class CompanyContactPersonWidget(FlexSelectWidget):
    The widget must extend FlexSelectWidget and implement trigger_fields, 
    details(), queryset() and empty_choices_text().

    trigger_fields = ['client']
    """Fields which on change will update the base field."""

    def details(self, base_field_instance, instance):
        HTML appended to the base_field.

        - base_field_instance: An instance of the base_field.
        - instance: A partial instance of the parent model loaded from the

        Returns a unicoded string.
        return u"""\
        """ % ('Company',,

    def queryset(self, instance):
        Returns the QuerySet populating the base field. If either of the
        trigger_fields is None, this function will not be called.

        - instance: A partial instance of the parent model loaded from the
        company =
        return CompanyContactPerson.objects.filter(company=company)

    def empty_choices_text(self, instance):
        If either of the trigger_fields is None this function will be called
        to get the text for the empty choice in the select box of the base

        - instance: A partial instance of the parent model loaded from the
        return "Please update the client field"

class CaseAdmin(admin.ModelAdmin):
    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        Alters the widget displayed for the base field.
        if == "company_contact_person":
            kwargs['widget'] =  CompanyContactPersonWidget(
            kwargs['label'] = 'Contact'
        return super(CaseAdmin, self).formfield_for_foreignkey(db_field, 
            request, **kwargs)

class ClientAdmin(admin.ModelAdmin):

class CompanyContactPersonAdmin(admin.ModelAdmin):

class CompanyAdmin(admin.ModelAdmin):
    pass, CaseAdmin), ClientAdmin), CompanyContactPersonAdmin), CompanyAdmin)


sudo pip install django-admin-flexselect


  1. Add "flexselect", to INSTALLED_APPS in "".

  2. Add url(r'^flexselect/', include('flexselect.urls')), to "".


As of yet, flexselect only have one configuration option, namely "include_jquery" that if set to true will include jQuery and jQueryUI from the google ajax CDN. It defaults to false and the entire FLEXSELECT dict can be omitted.

# Flexselect settings.
    'include_jquery': True,

Static files

FlexSelect requires "django.contrib.staticfiles" installed to work out of the box. If it is not then include "jQuery", "jQueryUI" and "flexselect/static/flexselect/js/flexselect.js" manually. Read more about "django.contrib.staticfiles" at

Something went wrong with that request. Please try again.