django - How to setup the initial value in select control? -


i'm trying setting select control on form, not achieving expected results. me, strangest thing working in previous control, same type.

this function involved:

class proofmspe(crearevidencia):     model = vrl02     form_class = vrl02form      def get_form(self, form_class):         form = super(proofmspe, self).get_form(form_class)         form.fields['miembro'].queryset = self.pipol         if self.pipol.count() == 1:             form.fields['miembro'].widget.initial = [self.pipol[0].id]         form.fields['meta'].initial = self.meta         form.fields['meta'].widget.attrs['disabled'] = true         return form 

the meta's control select , got expected behavior, ie automatically selects initial value (form.fields['meta'].initial = self.meta , inthe next lines, disabled (form.fields ['meta']. widget.attrs ['disabled'] = true). output in rendered template:

<!-- begin meta--> <div class="row">   <div class="col s12 input-field">     <select id="id_meta" name="meta" disabled>       <option value="">---------</option>       <option value="1" selected="selected">joce-1</option>       <option value="2">vel-1</option>       <option value="3">vel-2</option>       <option value="4">vel-3</option>     </select>     <label for="id_meta">evidencia para la meta</label>   </div> </div> <!-- end ./meta --> 

on other hand, pipol field i'm unable same result. difference, way, field has logic: filtered list of people same criteria , widget create whit list (form.fields['miembro'].queryset = self.pipol).

so far good, if queryset has one result (if self.pipol.count () == 1 :) want 1 used inital value (form.fields ['member']. widget.initial = [self .pipol [0] .id]), not working.

this appears when template rendered:

<!-- begin pipol--> <div class="row">   <div class="col s12 input-field">     <select id="id_miembro" name="miembro">       <option value="" selected="selected">---------</option>       <option value="2">***@***.mx</option>     </select>     <label for="id_miembro">seleccione el usuario</label>   </div> </div> <!-- end ./pipol --> 

thanks time.

you have set initial value form.field['miembro'] , not widget, did form.fields['meta'].

def get_form(self, form_class):     form = super(proofmspe, self).get_form(form_class)     form.fields['miembro'].queryset = self.pipol     if self.pipol.count() == 1:         # line here         form.fields['miembro'].initial = self.pipol[0]     form.fields['meta'].initial = self.meta     form.fields['meta'].widget.attrs['disabled'] = true     return form 

select output be:

<select id="id_miembro" name="miembro">   <option value="">---------</option>   <option value="2" selected="selected">***@***.mx</option> </select> 

Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -