Package: django-selectable-select2
WarningThis is still a work in progress. Some backwards incompatible changes may happen between releases.This project is a kind of a plugin fordjango-selectable.It provides widgets for use with a great JS library calledselect2rather than jQuery UI.For now there’s only a basic single-valued autocomplete widget for usage on ForeignKey (or simply ModelChoiceField) fields.Installationinstalldjango-selectable(you can ommit the part regarding jquery-ui)installdjango-selectable-select2like so:pip install django-selectable-select2addselectable_select2toINSTALLED_APPS. So it look like this:INSTALLED_APPS = (
    ...
    'selectable',
    'selectable_select2',
    ...
)add/change a settingSELECTABLE_ESCAPED_KEYSlike this:SELECTABLE_ESCAPED_KEYS = ('label', 'value')You can also get all the static files dependencies like this:pip install django-staticfiles-jquery
pip install django-staticfiles-select2and add them toINSTALLED_APPS:INSTALLED_APPS = (
    ...
    'jquery',
    'staticfiles_select2',
    ...
)Usagedefine yourlookup classin your forms you can useselectable_select2.widgets.AutoCompleteSelect2Widgetlike so:from selectable_select2.widgets import AutoCompleteSelect2Widget
from django import forms

from myapp.models import MyModel  # example model with a ForeignKey called ``myfk``
from myapp.lookups import MyModelLookup  # the lookup defined in previous step

class MyModelForm(forms.ModelForm):

    class Meta:
        model = MyModel
        widgets = {
            'myfk' : AutoCompleteSelect2Widget(MyModelLookup, placeholder='select related item')
        }How to include static assets?WarningAs of version 0.4.0django-selectable-select2doesn’t include any static files dependencies for select2 itself.
Usedjango-staticfiles-select2and/ordjango-staticfiles-jqueryif you don’t have them already in your project.You can mannually include those assets (assuming you’re using django-staticfiles). Like so:<html>
    <head>
        <link rel="stylesheet" href="{{ STATIC_URL }}staticfiles_select2/select2/select2.css">
    </head>

    <body>
        <form action="." method="post">
            {{ form.as_p }}
            <p><button type="submit">Submit</button></p>
        </form>

        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
        <script type="text/javascript" src="{{ STATIC_URL }}staticfiles_select2/select2/select2.min.js"></script>
        <script type="text/javascript" src="{{ STATIC_URL }}selectable_select2/js/jquery.dj.selectable.select2.js"></script>
    </body>
</html>Chained selectsThere is a way to do chained selects indjango-selectable.
Check out thedocs about chained selectsto correctly prepare your lookup classes
for this use case (you can skip the javascript part).
Django-selectable-select2 provides a helper class to declare dependencies of your chained selects
on your form.So given the lookup, from the above link and assuming that MyModel has ForeignKeys
for city and state, your form class can inherit fromSelect2DependencyModelFormand defineselect2_depsattribute like this:from selectable_select2.forms import Select2DependencyModelForm
from django import forms
from selectable_select2.widgets import AutoCompleteSelect2Widget

class ChainedForm(Select2DependencyModelForm):

    select2_deps = (
        ('city', { 'parents' : ['state'] }),
    )

    class Meta:
        model = MyModel
        widgets = {
            'city' : AutoCompleteSelect2Widget(CityLookup, placeholder='select city')
        }There is alsoSelect2DependencyFormwhich is suitable for non-model based forms.NoteBothSelect2DependencyModelFormandSelect2DependencyForminselectable_select2.formsmodule inherit from a general class calledSelect2DependencyFormMixinwhich defines one method calledapply_select2_deps.
Don’t hesitate to browse the source of those classes.select2_depsis a tuple of two-tuples in form(‘<fieldname>’ : { <options dict> })where theoptions dictis a Python dictionary that configurates the dependencies for that field.Reference for theoptions dict:parentsList of field names that are superior for the given field.
Like in the above example you can choose acitydepending on whatstateyou’ve chosen.
The field can be dependant from more than one parent. Defaults to:[].clearonparentchangeBoolean (True/False) that indicates whether a field should be cleared when a user
changes the selection/value of one of it’s parents. Defaults to:True.parents_namemapA convenient option (python dictionary) for indicating which key name is sent via ajax for which parent.
E.g. Assume that fieldchilddepends onparent1andparent2in our
chained selects. You can specify:select2_deps = (
    ('child', {
        parents : ['parent1', 'parent2' ]
        parents_namemap : { 'parent1' : 'parent', 'parent2' : 'parent' }
    }),
)Then your lookup can be cleaner and you can search only forparentkey
instead of juggling withparent1andparent2in yourget_querymethod. Defaults to:{}.Check theexampleproject for more details.TODOcheck outTODO.rstA note about version of django-selectableThe minimal version of django-selectable that is required for this app is 0.7CreditsA BIG THANK YOU goes toIgor Vaynberg(select2) andMark Lavin(django-selectable)
for their projects, their support and quick response time in resolving my issues.