Note that there are some explanatory texts on larger screens.

plurals
  1. POSelection of columns in a modelChoiceField queryset: output messed up
    text
    copied!<p>I have a modelChoiceField created with Django:</p> <pre><code>lawsToValidate=forms.ModelChoiceField(queryset=LawsIdsModel.objects.filter(validated=0).values("releveAnnee", "releveMois", "noOrdre"), widget=forms.Select(attrs={'onchange': 'javascript: lawToValidateDisplay(this.value);'})) </code></pre> <p>The django code creates this html code:</p> <pre><code>&lt;option value="{'releveMois': 1L, 'noOrdre': 2L, 'releveAnnee': 2009L}"&gt;{'releveMois': 1L, 'noOrdre': 2L, 'releveAnnee': 2009L}&lt;/option&gt; </code></pre> <p>The django code calls my jquery function right after the click on the drop down list. If I display the selected value in my javascript function, here is what I have: </p> <pre><code>{'releveMois': 1L, 'noOrdre': 3L, 'releveAnnee': 2009L} </code></pre> <p>How do I get the information from this python dictionnary in jquery / javascript. How do I get releveMois, releveAnnee, noOrdre? If I try jQuery.parseJSON, it does not work!</p> <p><strong>EDIT:</strong> With the comments below, I understood that it was better to change the html output. Here is my template code to generate the drop down list:</p> <pre><code>&lt;div id="lawsToValidateChoice" class="fieldWrapper"&gt; {{ form.lawsToValidate.errors }} &lt;select onchange="javascript: lawToValidateDisplay(this.value);" name="lawsToValidate" id="id_lawsToValidate"&gt; &lt;option value="" selected="selected"&gt;Select a law to validate&lt;/option&gt; {% for law in form.lawsToValidate.field.queryset %} &lt;option value="{{ law.releveAnnee }},{{ law.releveMois }},{{ law.noOrdre }}"&gt;releveAnnee={{ law.releveAnnee }}, releveMois={{ law.releveMois }}, noOrdre={{ law.noOrdre }}&lt;/option&gt; {% endfor %} &lt;/select&gt; &lt;/div&gt; </code></pre> <p>No problem anymore to parse the values! Solved!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload