5. Reference

5.1. Widgets

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

Implement 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.

get_options()[source]

Return dictionary of options to be used by Select2.

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

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

render_js_code(id_, *args, **kwargs)[source]

Render html container for Select2 widget with options.

render_select2_options_code(options, id_)[source]

Render options for select2.

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

Implement 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 create 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]

Return 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) and must be of type dict.

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

Return ModelForm class for model with select2 widgets.

Arguments:
attrs: select2 widget attributes (width, for example) of type dict. 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)

Return 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) and must be of type dict.