5. Reference

5.1. Widgets

class easy_select2.widgets.Select2(select2attrs=None, *args, **kwargs)[source]

Implements single-valued select widget with Select2.

class easy_select2.widgets.Select2Mixin(select2attrs=None, *args, **kwargs)[source]

This mixin provides a mechanism to construct custom widget class, that will be rendered using Select2 input.

Generally should be mixed with widgets that render select input.

render(*args, **kwargs)[source]

Extends base class’s render method by appending javascript inline text to html output.

class easy_select2.widgets.Select2Multiple(select2attrs=None, *args, **kwargs)[source]

Implements multiple select widget with Select2.

class easy_select2.widgets.Select2TextInput(select2attrs=None, *args, **kwargs)[source]

A Select2-enabled combo box for non-choice fields which can provide a list of pre-set choices, or can accept arbitrary input.

To use this, do NOT set a choices attribute on the model field, but DO supply a data attribute to select2attrs that contains a list of dictionaries each having at least an id and text terms like so:

form.fields['myfield'].widget = Select2TextInput(
    select2attrs={
        'data': [ {'id': 'your data', 'text': 'your data'}, ... ],
    },
)
class easy_select2.widgets.Select2TextMixin(select2attrs=None, *args, **kwargs)[source]

This mixin provides a mechanism to construct custom widget class, that will be rendered using Select2.

It will work as Select2Mixin if there is no data attribute in select2attrs. If data attribute is passed, Select2 will be configured to use pre-set list of choices.

Generally should be mixed with widgets, that renders as text input.

5.2. Utils

easy_select2.utils.apply_select2(widget_cls)[source]

Dynamically creates new widget class mixed with Select2Mixin.

Args:
widget_cls: class of source widget.

Usage, for example:

class SomeModelForm(admin.ModelForm):
    class Meta:
        widgets = {
            'field': apply_select2(forms.Select),
        }

So, apply_select2(forms.Select) will return new class, named Select2Select.

easy_select2.utils.select2_meta_factory(model, meta_fields=None, widgets=None, attrs=None)[source]

Returns Meta class with Select2-enabled widgets for fields with choices (e.g. ForeignKey, CharField, etc) for use with ModelForm.

Attrs argument is select2 widget attributes (width, for example).

easy_select2.utils.select2_modelform(model, attrs=None, form_class=<class 'django.forms.models.ModelForm'>)[source]

Returns ModelForm class for model with select2 widgets.

Arguments:
attrs: select2 widget attributes (width, for example). form_class: modelform base class, forms.ModelForm by default.
SomeModelForm = select2_modelform(models.SomeModelBanner)

is the same like:

class SomeModelForm(forms.ModelForm):
    Meta = select2_modelform_meta(models.SomeModelForm)
easy_select2.utils.select2_modelform_meta(model, meta_fields=None, widgets=None, attrs=None)

Returns Meta class with Select2-enabled widgets for fields with choices (e.g. ForeignKey, CharField, etc) for use with ModelForm.

Attrs argument is select2 widget attributes (width, for example).