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(name, value, attrs=None, **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.

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_modelform(model, attrs=None, form_class=<class 'easy_select2.forms.FixedModelForm'>)[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, **kwargs)[source]

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

Arguments:

model - a model class to create Meta class for. meta_fields - dictionary with Meta class fields, for

example, {‘fields’: [‘id’, ‘name’]}
attrs - select2 widget attributes (width, for example),
must be of type dict.

**kwargs - will be merged with meta_fields.