Django - dropdown form with multiple select
<p><strong>I need guidance building a django <em>dropdown</em> <code>forms.Form</code> field in which I can select <em>multiple</em> choices.  I need to select multiple locations on the <code>office</code> field of the form.</strong></p>

<p>When submitted, the form needs to return a <code>list</code> of the chosen offices (e.g. <code>["New York", "Los Angeles"]</code> or <code>["Austin"]</code>).  Returning a <code>tuple</code> is also acceptable.</p>

<hr>

<p>The best I can do right now is build a <a href="https://docs.djangoproject.com/en/1.10/ref/forms/fields/#multiplechoicefield" rel="nofollow">multipleChoiceField</a> for <code>office</code> with the following:</p>

<pre><code>from django import forms

class my_Form(forms.Form):

    OPTIONS = [
        ("0", "*ALL"),
        ("1", "New York"),
        ("2", "Los Angeles"),
        ]

    office = forms.MultipleChoiceField(
            choices=OPTIONS,
            initial='0',
            widget=forms.SelectMultiple(),
            required=True,
            label='Office',
        )
</code></pre>

<p>resulting in this form field layout:</p>

<p><a href="http://i.stack.imgur.com/7OZIH.png" rel="nofollow"><img src="http://i.stack.imgur.com/7OZIH.png" alt="enter image description here"></a></p>

<hr>

<p>However, I would like this field to be a dropdown, to take up less space on the page.</p>

<p>I found this <a href="https://djangosnippets.org/snippets/747/" rel="nofollow">djangosnippet</a> but (1) a few people have mentioned it appears out of date (I can't confirm), and (2) I first want to check if a built-in django form/widget setup can fulfill this task before using custom code.</p>

<p>You can choose multiple choices by using Django select2. Include below code in your respective HTML file.</p>

<pre><code>&lt;link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/css/select2.min.css" rel="stylesheet" /&gt;
&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/js/select2.min.js"&gt;&lt;/script&gt;

&lt;select class="select_field_class" multiple="multiple" name="option" id="option"&gt;
      &lt;option value=""&gt;Enter the option&lt;/option&gt;
      {% for option in options %}
         &lt;option value="{{ option.id }}"&gt;{{ option.name }}&lt;/option&gt;
      {% endfor %}
&lt;/select&gt;

$('.select_field_class').select2( { placeholder: "Select here", maximumSelectionSize: 100  } );
</code></pre>

python django forms