Skip to main content

plugin for django-selectable (to use with select2 UI)

Project description

This project is a kind of a plugin for django-selectable.

It provides widgets for use with a great JS library called select2 rather than jQuery UI.

For now there’s only a basic single-valued autocomplete widget for usage on ForeignKey (or simply ModelChoiceField) fields.

Installation

  • install django-selectable (you can ommit the part regarding jquery-ui)

  • install django-selectable-select2 like so:

    pip install django-selectable-select2
  • add selectable_select2 to INSTALLED_APPS. So it look like this:

    INSTALLED_APPS = (
        ...
        'selectable',
        'selectable_select2',
        ...
    )
  • add/change a setting SELECTABLE_ESCAPED_KEYS like 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-select2

and add them to INSTALLED_APPS:

INSTALLED_APPS = (
    ...
    'jquery',
    'staticfiles_select2',
    ...
)

Usage

  • define your lookup class

  • in your forms you can use selectable_select2.widgets.AutoCompleteSelect2Widget like 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?

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 selects

There is a way to do chained selects in django-selectable. Check out the docs about chained selects to 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 from Select2DependencyModelForm and define select2_deps attribute 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 also Select2DependencyForm which is suitable for non-model based forms.

select2_deps is a tuple of two-tuples in form (‘<fieldname>’ : { <options dict> }) where the options dict is a Python dictionary that configurates the dependencies for that field.

Reference for the options dict:

parents

List of field names that are superior for the given field. Like in the above example you can choose a city depending on what state you’ve chosen. The field can be dependant from more than one parent. Defaults to: [].

clearonparentchange

Boolean (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_namemap

A convenient option (python dictionary) for indicating which key name is sent via ajax for which parent. E.g. Assume that field child depends on parent1 and parent2 in 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 for parent key instead of juggling with parent1 and parent2 in your get_query method. Defaults to: {}.

Check the example project for more details.

TODO

check out TODO.rst

A note about version of django-selectable

The minimal version of django-selectable that is required for this app is 0.7

Credits

A BIG THANK YOU goes to Igor Vaynberg (select2) and Mark Lavin (django-selectable) for their projects, their support and quick response time in resolving my issues.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-selectable-select2-0.4.0.tar.gz (8.9 kB view details)

Uploaded Source

File details

Details for the file django-selectable-select2-0.4.0.tar.gz.

File metadata

File hashes

Hashes for django-selectable-select2-0.4.0.tar.gz
Algorithm Hash digest
SHA256 40e4926f554e9d6a31ff5d51c333f541b77d957f790128aaab42245b680014ee
MD5 ad617795a48efa8e3dbff44e9804220d
BLAKE2b-256 5e00217ba5ccf3168374cdb90ba6a258bcdf2f7a63dd7067e4c8c6680a9bf248

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page