<imgsrc="_images/inheritance-13782e5f674c6b31f4c4c1b27e4a0349bdac7cc8.png"alt="Inheritance diagram of django_select2.widgets"usemap="#inheritancecdbd4d640c"class="inheritance"/>
<emclass="property">class </em><ttclass="descclassname">django_select2.widgets.</tt><ttclass="descname">Select2Mixin</tt><big>(</big><em>**kwargs</em><big>)</big><aclass="reference internal"href="_modules/django_select2/widgets.html#Select2Mixin"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#django_select2.widgets.Select2Mixin"title="Permalink to this definition">¶</a></dt>
<p>This mixin is responsible for rendering the necessary Javascript and CSS codes which turns normal <ttclass="docutils literal"><spanclass="pre"><select></span></tt>
markups into Select2 choice list.</p>
<p>The following Select2 otions are added by this mixin:-</p>
<ttclass="descname">__init__</tt><big>(</big><em>**kwargs</em><big>)</big><aclass="reference internal"href="_modules/django_select2/widgets.html#Select2Mixin.__init__"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#django_select2.widgets.Select2Mixin.__init__"title="Permalink to this definition">¶</a></dt>
<trclass="field-odd field"><thclass="field-name">Parameters:</th><tdclass="field-body"><strong>select2_options</strong> (<aclass="reference external"href="http://docs.python.org/2.7/library/stdtypes.html#dict"title="(in Python v2.7)"><ttclass="xref py py-obj docutils literal"><spanclass="pre">dict</span></tt></a> or None) –<p>This is similar to standard Django way to pass extra attributes to widgets.
This is meant to override values of existing <aclass="reference internal"href="#django_select2.widgets.Select2Mixin.options"title="django_select2.widgets.Select2Mixin.options"><ttclass="xref py py-attr docutils literal"><spanclass="pre">options</span></tt></a>.</p>
<p>You cannot introduce new options using this. For that you should sub-class and overried
<aclass="reference internal"href="#django_select2.widgets.Select2Mixin.init_options"title="django_select2.widgets.Select2Mixin.init_options"><ttclass="xref py py-meth docutils literal"><spanclass="pre">init_options()</span></tt></a>. The reason for this is, few options are not compatible with each other
or are not applicable in some scenarios. For example, when Select2 is attached to <ttclass="docutils literal"><spanclass="pre"><select></span></tt> tag,
it can get if it is multiple or single valued from that tag itself. In this case if you specify
<ttclass="docutils literal"><spanclass="pre">multiple</span></tt> option then not only it is useless but an error in Select2 JS’ point of view.</p>
<pclass="last">There are other such intricacies, based on which some options are removed. By enforcing this
restriction we make sure to not break the code by passing some wrong concotion of options.</p>
<ttclass="descname">options</tt><emclass="property"> = {'minimumResultsForSearch': 6, 'allowClear': True, 'closeOnSelect': False, 'placeholder': '', 'multiple': False}</em><aclass="headerlink"href="#django_select2.widgets.Select2Mixin.options"title="Permalink to this definition">¶</a></dt>
<dd><p>The options listed in this are rendered as JS map and passed to Select2 JS code.
The complete description of theses options are available in <aclass="reference external"href="http://ivaynberg.github.com/select2/#documentation.">Select2</a> JS’ site.</p>
<ttclass="descname">init_options</tt><big>(</big><big>)</big><aclass="reference internal"href="_modules/django_select2/widgets.html#Select2Mixin.init_options"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#django_select2.widgets.Select2Mixin.init_options"title="Permalink to this definition">¶</a></dt>
<p>In the above example we are setting <ttclass="docutils literal"><spanclass="pre">Your_js_function</span></tt> as Select2’s <ttclass="docutils literal"><spanclass="pre">createSearchChoice</span></tt>
function.</p>
<divclass="admonition tip">
<pclass="first admonition-title">Tip</p>
<pclass="last">If you want to run <ttclass="docutils literal"><spanclass="pre">Your_js_function</span></tt> in the context of the Select2 DOM element,
i.e. <ttclass="docutils literal"><spanclass="pre">this</span></tt> inside your JS function should point to the component instead of <ttclass="docutils literal"><spanclass="pre">window</span></tt>, then
use <aclass="reference internal"href="ref_util.html#django_select2.util.JSFunctionInContext"title="django_select2.util.JSFunctionInContext"><ttclass="xref py py-class docutils literal"><spanclass="pre">JSFunctionInContext</span></tt></a> instead of <aclass="reference internal"href="ref_util.html#django_select2.util.JSFunction"title="django_select2.util.JSFunction"><ttclass="xref py py-class docutils literal"><spanclass="pre">JSFunction</span></tt></a>.</p>
<ttclass="descname">set_placeholder</tt><big>(</big><em>val</em><big>)</big><aclass="reference internal"href="_modules/django_select2/widgets.html#Select2Mixin.set_placeholder"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#django_select2.widgets.Select2Mixin.set_placeholder"title="Permalink to this definition">¶</a></dt>
<dd><p>Placeholder is a value which Select2 JS library shows when nothing is selected. This should be string.</p>
<ttclass="descname">get_options</tt><big>(</big><big>)</big><aclass="reference internal"href="_modules/django_select2/widgets.html#Select2Mixin.get_options"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#django_select2.widgets.Select2Mixin.get_options"title="Permalink to this definition">¶</a></dt>
<ttclass="descname">render_select2_options_code</tt><big>(</big><em>options</em>, <em>id_</em><big>)</big><aclass="reference internal"href="_modules/django_select2/widgets.html#Select2Mixin.render_select2_options_code"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#django_select2.widgets.Select2Mixin.render_select2_options_code"title="Permalink to this definition">¶</a></dt>
<ttclass="descname">render_js_code</tt><big>(</big><em>id_</em>, <em>*args</em><big>)</big><aclass="reference internal"href="_modules/django_select2/widgets.html#Select2Mixin.render_js_code"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#django_select2.widgets.Select2Mixin.render_js_code"title="Permalink to this definition">¶</a></dt>
<dd><p>Renders the <ttclass="docutils literal"><spanclass="pre"><script></span></tt> block which contains the JS code for this widget.</p>
<ttclass="descname">render_inner_js_code</tt><big>(</big><em>id_</em>, <em>*args</em><big>)</big><aclass="reference internal"href="_modules/django_select2/widgets.html#Select2Mixin.render_inner_js_code"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#django_select2.widgets.Select2Mixin.render_inner_js_code"title="Permalink to this definition">¶</a></dt>
<dd><p>Renders all the JS code required for this widget.</p>
<trclass="field-odd field"><thclass="field-name">Returns:</th><tdclass="field-body">The rendered JS code which will be later enclosed inside <ttclass="docutils literal"><spanclass="pre"><script></span></tt> block.</td>
<ttclass="descname">render</tt><big>(</big><em>name</em>, <em>value</em>, <em>attrs=None</em>, <em>choices=()</em><big>)</big><aclass="reference internal"href="_modules/django_select2/widgets.html#Select2Mixin.render"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#django_select2.widgets.Select2Mixin.render"title="Permalink to this definition">¶</a></dt>
<dd><p>Renders this widget. Html and JS code blocks all are rendered by this.</p>
<emclass="property">class </em><ttclass="descclassname">django_select2.widgets.</tt><ttclass="descname">Select2Widget</tt><big>(</big><em>**kwargs</em><big>)</big><aclass="reference internal"href="_modules/django_select2/widgets.html#Select2Widget"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#django_select2.widgets.Select2Widget"title="Permalink to this definition">¶</a></dt>
<emclass="property">class </em><ttclass="descclassname">django_select2.widgets.</tt><ttclass="descname">Select2MultipleWidget</tt><big>(</big><em>**kwargs</em><big>)</big><aclass="reference internal"href="_modules/django_select2/widgets.html#Select2MultipleWidget"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#django_select2.widgets.Select2MultipleWidget"title="Permalink to this definition">¶</a></dt>
<emclass="property">class </em><ttclass="descclassname">django_select2.widgets.</tt><ttclass="descname">MultipleSelect2HiddenInput</tt><big>(</big><em>attrs=None</em><big>)</big><aclass="reference internal"href="_modules/django_select2/widgets.html#MultipleSelect2HiddenInput"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#django_select2.widgets.MultipleSelect2HiddenInput"title="Permalink to this definition">¶</a></dt>
<emclass="property">class </em><ttclass="descclassname">django_select2.widgets.</tt><ttclass="descname">HeavySelect2Mixin</tt><big>(</big><em>**kwargs</em><big>)</big><aclass="reference internal"href="_modules/django_select2/widgets.html#HeavySelect2Mixin"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#django_select2.widgets.HeavySelect2Mixin"title="Permalink to this definition">¶</a></dt>
<p>The base mixin of all Heavy Select2 widgets. It sub-classes <aclass="reference internal"href="#django_select2.widgets.Select2Mixin"title="django_select2.widgets.Select2Mixin"><ttclass="xref py py-class docutils literal"><spanclass="pre">Select2Mixin</span></tt></a>.</p>
<p>This mixin adds more Select2 options to <aclass="reference internal"href="#django_select2.widgets.Select2Mixin.options"title="django_select2.widgets.Select2Mixin.options"><ttclass="xref py py-attr docutils literal"><spanclass="pre">Select2Mixin.options</span></tt></a>. These are:-</p>
<pclass="last">You can override these options by passing <ttclass="docutils literal"><spanclass="pre">select2_options</span></tt> kwarg to <aclass="reference internal"href="#django_select2.widgets.HeavySelect2Mixin.__init__"title="django_select2.widgets.HeavySelect2Mixin.__init__"><ttclass="xref py py-meth docutils literal"><spanclass="pre">__init__()</span></tt></a>.</p>
<ttclass="descname">__init__</tt><big>(</big><em>**kwargs</em><big>)</big><aclass="reference internal"href="_modules/django_select2/widgets.html#HeavySelect2Mixin.__init__"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#django_select2.widgets.HeavySelect2Mixin.__init__"title="Permalink to this definition">¶</a></dt>
<li><strong>data_view</strong> (<aclass="reference external"href="https://docs.djangoproject.com/en/1.4/ref/class-based-views/#django.views.generic.base.View"title="(in Django v1.4.1)"><ttclass="xref py py-class docutils literal"><spanclass="pre">django.views.generic.base.View</span></tt></a> or None) – A <aclass="reference internal"href="ref_views.html#django_select2.views.Select2View"title="django_select2.views.Select2View"><ttclass="xref py py-class docutils literal"><spanclass="pre">Select2View</span></tt></a> sub-class which can respond to this widget’s Ajax queries.</li>
<li><strong>data_url</strong> (<aclass="reference external"href="http://docs.python.org/2.7/library/functions.html#str"title="(in Python v2.7)"><ttclass="xref py py-obj docutils literal"><spanclass="pre">str</span></tt></a> or None) – Url which will respond to Ajax queries with JSON object.</li>
<pclass="last">Either of <ttclass="docutils literal"><spanclass="pre">data_view</span></tt> or <ttclass="docutils literal"><spanclass="pre">data_url</span></tt> must be specified, else <ttclass="xref py py-exc docutils literal"><spanclass="pre">ValueError</span></tt> would
<li><strong>choices</strong> (<aclass="reference external"href="http://docs.python.org/2.7/library/functions.html#list"title="(in Python v2.7)"><ttclass="xref py py-obj docutils literal"><spanclass="pre">list</span></tt></a> or <aclass="reference external"href="http://docs.python.org/2.7/library/functions.html#tuple"title="(in Python v2.7)"><ttclass="xref py py-obj docutils literal"><spanclass="pre">tuple</span></tt></a>) – The list of available choices. If not provided then empty list is used instead. It
should be of the form –<ttclass="docutils literal"><spanclass="pre">[(val1,</span><spanclass="pre">'Label1'),</span><spanclass="pre">(val2,</span><spanclass="pre">'Label2'),</span><spanclass="pre">...]</span></tt>.</li>
<li><strong>userGetValTextFuncName</strong>–<p>The name of the custom JS function which you want to use to convert
value to label.</p>
<p>In <ttclass="docutils literal"><spanclass="pre">heavy_data.js</span></tt>, <ttclass="docutils literal"><spanclass="pre">django_select2.getValText()</span></tt> employs the following logic to convert value
to label :-</p>
<blockquote>
<div>1. First check if the Select2 input field has <ttclass="docutils literal"><spanclass="pre">txt</span></tt> attribute set along with <ttclass="docutils literal"><spanclass="pre">value</span></tt>. If found
then use it.<p>2. Otherwise, check if user has provided any custom method for this. Then use that. If it returns a
label then use it.</p>
<p>3. Otherwise, check the cached results. When the user searches in the fields then all the returned
responses from server, which has the value and label mapping, are cached by <ttclass="docutils literal"><spanclass="pre">heavy_data.js</span></tt>.</p>
<p>4. If we still do not have the label then check the cookies. When user selects some value then
<ttclass="docutils literal"><spanclass="pre">heavy_data.js</span></tt> stores the selected values and their labels in the cookies. These are cleared
<ttclass="descname">render_texts</tt><big>(</big><em>selected_choices</em>, <em>choices</em><big>)</big><aclass="reference internal"href="_modules/django_select2/widgets.html#HeavySelect2Mixin.render_texts"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#django_select2.widgets.HeavySelect2Mixin.render_texts"title="Permalink to this definition">¶</a></dt>
<li><strong>selected_choices</strong> (<aclass="reference external"href="http://docs.python.org/2.7/library/functions.html#list"title="(in Python v2.7)"><ttclass="xref py py-obj docutils literal"><spanclass="pre">list</span></tt></a> or <aclass="reference external"href="http://docs.python.org/2.7/library/functions.html#tuple"title="(in Python v2.7)"><ttclass="xref py py-obj docutils literal"><spanclass="pre">tuple</span></tt></a>) – List of selected choices’ values.</li>
<li><strong>choices</strong> (<aclass="reference external"href="http://docs.python.org/2.7/library/functions.html#list"title="(in Python v2.7)"><ttclass="xref py py-obj docutils literal"><spanclass="pre">list</span></tt></a> or <aclass="reference external"href="http://docs.python.org/2.7/library/functions.html#tuple"title="(in Python v2.7)"><ttclass="xref py py-obj docutils literal"><spanclass="pre">tuple</span></tt></a>) – Extra choices, if any. This is a list of tuples. In each tuple, the first
<ttclass="descname">render_texts_for_value</tt><big>(</big><em>id_</em>, <em>value</em>, <em>choices</em><big>)</big><aclass="reference internal"href="_modules/django_select2/widgets.html#HeavySelect2Mixin.render_texts_for_value"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#django_select2.widgets.HeavySelect2Mixin.render_texts_for_value"title="Permalink to this definition">¶</a></dt>
<dd><p>Renders the JS code which sets the <ttclass="docutils literal"><spanclass="pre">txt</span></tt> attribute on the field. It gets the array
of lables from <aclass="reference internal"href="#django_select2.widgets.HeavySelect2Mixin.render_texts"title="django_select2.widgets.HeavySelect2Mixin.render_texts"><ttclass="xref py py-meth docutils literal"><spanclass="pre">render_texts()</span></tt></a>.</p>
<li><strong>id</strong> (<aclass="reference external"href="http://docs.python.org/2.7/library/functions.html#str"title="(in Python v2.7)"><ttclass="xref py py-obj docutils literal"><spanclass="pre">str</span></tt></a>) – Id of the field. This can be used to get reference of this field’s DOM in JS.</li>
<li><strong>choices</strong> (<aclass="reference external"href="http://docs.python.org/2.7/library/functions.html#list"title="(in Python v2.7)"><ttclass="xref py py-obj docutils literal"><spanclass="pre">list</span></tt></a> or <aclass="reference external"href="http://docs.python.org/2.7/library/functions.html#tuple"title="(in Python v2.7)"><ttclass="xref py py-obj docutils literal"><spanclass="pre">tuple</span></tt></a>) – Extra choices, if any. This is a list of tuples. In each tuple, the first
item is the choice value and the second item is choice label.</li>
</ul>
</td>
</tr>
<trclass="field-even field"><thclass="field-name">Returns:</th><tdclass="field-body"><pclass="first">JS code which sets the <ttclass="docutils literal"><spanclass="pre">txt</span></tt> attribute.</p>
<emclass="property">class </em><ttclass="descclassname">django_select2.widgets.</tt><ttclass="descname">HeavySelect2Widget</tt><big>(</big><em>**kwargs</em><big>)</big><aclass="reference internal"href="_modules/django_select2/widgets.html#HeavySelect2Widget"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#django_select2.widgets.HeavySelect2Widget"title="Permalink to this definition">¶</a></dt>
<p>Following Select2 option from <aclass="reference internal"href="#django_select2.widgets.Select2Mixin.options"title="django_select2.widgets.Select2Mixin.options"><ttclass="xref py py-attr docutils literal"><spanclass="pre">Select2Mixin.options</span></tt></a> is added or set:-</p>
<emclass="property">class </em><ttclass="descclassname">django_select2.widgets.</tt><ttclass="descname">HeavySelect2MultipleWidget</tt><big>(</big><em>**kwargs</em><big>)</big><aclass="reference internal"href="_modules/django_select2/widgets.html#HeavySelect2MultipleWidget"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#django_select2.widgets.HeavySelect2MultipleWidget"title="Permalink to this definition">¶</a></dt>
<p>Following Select2 options from <aclass="reference internal"href="#django_select2.widgets.Select2Mixin.options"title="django_select2.widgets.Select2Mixin.options"><ttclass="xref py py-attr docutils literal"><spanclass="pre">Select2Mixin.options</span></tt></a> are removed:-</p>
<blockquote>
<div><ulclass="simple">
<li>allowClear</li>
<li>minimumResultsForSearch</li>
</ul>
</div></blockquote>
<p>Following Select2 options from <aclass="reference internal"href="#django_select2.widgets.Select2Mixin.options"title="django_select2.widgets.Select2Mixin.options"><ttclass="xref py py-attr docutils literal"><spanclass="pre">Select2Mixin.options</span></tt></a> are added or set:-</p>
<ttclass="descname">render_texts_for_value</tt><big>(</big><em>id_</em>, <em>value</em>, <em>choices</em><big>)</big><aclass="reference internal"href="_modules/django_select2/widgets.html#HeavySelect2MultipleWidget.render_texts_for_value"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#django_select2.widgets.HeavySelect2MultipleWidget.render_texts_for_value"title="Permalink to this definition">¶</a></dt>
<dd><p>Renders the JS code which sets the <ttclass="docutils literal"><spanclass="pre">txt</span></tt> attribute on the field. It gets the array
of lables from <aclass="reference internal"href="#django_select2.widgets.HeavySelect2Mixin.render_texts"title="django_select2.widgets.HeavySelect2Mixin.render_texts"><ttclass="xref py py-meth docutils literal"><spanclass="pre">render_texts()</span></tt></a>.</p>
<li><strong>id</strong> (<aclass="reference external"href="http://docs.python.org/2.7/library/functions.html#str"title="(in Python v2.7)"><ttclass="xref py py-obj docutils literal"><spanclass="pre">str</span></tt></a>) – Id of the field. This can be used to get reference of this field’s DOM in JS.</li>
<li><strong>value</strong> (<aclass="reference external"href="http://docs.python.org/2.7/library/functions.html#list"title="(in Python v2.7)"><ttclass="xref py py-obj docutils literal"><spanclass="pre">list</span></tt></a>) –<strong>List</strong> of currently set value on the field.</li>
<li><strong>choices</strong> (<aclass="reference external"href="http://docs.python.org/2.7/library/functions.html#list"title="(in Python v2.7)"><ttclass="xref py py-obj docutils literal"><spanclass="pre">list</span></tt></a> or <aclass="reference external"href="http://docs.python.org/2.7/library/functions.html#tuple"title="(in Python v2.7)"><ttclass="xref py py-obj docutils literal"><spanclass="pre">tuple</span></tt></a>) – Extra choices, if any. This is a list of tuples. In each tuple, the first
item is the choice value and the second item is choice label.</li>
</ul>
</td>
</tr>
<trclass="field-even field"><thclass="field-name">Returns:</th><tdclass="field-body"><pclass="first">JS code which sets the <ttclass="docutils literal"><spanclass="pre">txt</span></tt> attribute.</p>
<emclass="property">class </em><ttclass="descclassname">django_select2.widgets.</tt><ttclass="descname">AutoHeavySelect2Mixin</tt><aclass="reference internal"href="_modules/django_select2/widgets.html#AutoHeavySelect2Mixin"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#django_select2.widgets.AutoHeavySelect2Mixin"title="Permalink to this definition">¶</a></dt>
<p>This mixin is needed for Auto heavy fields.</p>
<p>This mxin adds extra JS code to notify the field’s DOM object of the generated id. The generated id
is not the same as the <ttclass="docutils literal"><spanclass="pre">id</span></tt> attribute of the field’s Html markup. This id is generated by
<aclass="reference internal"href="ref_util.html#django_select2.util.register_field"title="django_select2.util.register_field"><ttclass="xref py py-func docutils literal"><spanclass="pre">register_field()</span></tt></a> when the Auto field is registered. The client side (DOM) sends this
id along with the Ajax request, so that the central view can identify which field should be used to
<emclass="property">class </em><ttclass="descclassname">django_select2.widgets.</tt><ttclass="descname">AutoHeavySelect2Widget</tt><big>(</big><em>**kwargs</em><big>)</big><aclass="reference internal"href="_modules/django_select2/widgets.html#AutoHeavySelect2Widget"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#django_select2.widgets.AutoHeavySelect2Widget"title="Permalink to this definition">¶</a></dt>
<p>Auto version of <aclass="reference internal"href="#django_select2.widgets.HeavySelect2Widget"title="django_select2.widgets.HeavySelect2Widget"><ttclass="xref py py-class docutils literal"><spanclass="pre">HeavySelect2Widget</span></tt></a></p>
<emclass="property">class </em><ttclass="descclassname">django_select2.widgets.</tt><ttclass="descname">AutoHeavySelect2MultipleWidget</tt><big>(</big><em>**kwargs</em><big>)</big><aclass="reference internal"href="_modules/django_select2/widgets.html#AutoHeavySelect2MultipleWidget"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#django_select2.widgets.AutoHeavySelect2MultipleWidget"title="Permalink to this definition">¶</a></dt>
<p>Auto version of <aclass="reference internal"href="#django_select2.widgets.HeavySelect2MultipleWidget"title="django_select2.widgets.HeavySelect2MultipleWidget"><ttclass="xref py py-class docutils literal"><spanclass="pre">HeavySelect2MultipleWidget</span></tt></a></p>