How to dynamically replace some field in Flask-Admin into Select2?
<p>I have a flask-admin form, where when user select the topmost dropdown field, certain fields in this form must change into Select2 (dropdown) field. This is what I have so far:</p>

<pre><code>for(var idx in formData.custom_field_chooser)
    {
        var custom_field = $('#' + idx);
        custom_field.select2(); //This generate error
    }
</code></pre>

<p>I think my code is correct, but that <code>custom_field.select2()</code> code geneate this error <code>Uncaught query function not defined for Select2 undefined</code>.</p>

<p>Now, I am sure Select2 has already been included by <code>flask-admin</code> (cmiiw), but how do we use that?</p>

<p>Finally looking at the trivial error message <code>Uncaught query function not defined for Select2</code>, <a href="http://stackoverflow.com/questions/14483348/query-function-not-defined-for-select2-undefined-error">this post in SO</a> solved my problem. This is how you properly initialized an empty Select2:</p>

<pre><code>custom_field.select2({
 data: {
   id: "",
   text: ""
  }
 });
</code></pre>

<p>This will change that <code>custom_field</code> element into <code>select2</code>.</p>

python twitter-bootstrap-3 select2 flask-admin