#30014: Initialising disabled ModelChoiceField yields 'Select a valid 
choice'-error
despite initialised option being valid
-------------------------------------+-------------------------------------
     Reporter:  thoha                |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Forms                |                  Version:  1.11
     Severity:  Normal               |               Resolution:
     Keywords:  forms, disabled      |             Triage Stage:
  field, error, to_field_name        |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by thoha):

 Because this is for work on a commercial project, I can't give the exact
 code, but I'll try to provide generic examples.

 The form field get intialised in the FormView via:

 {{{
     def get_initial(self):
         """
         Returns the initial data to use for forms on this view.
         """
         initial = super(FormView, self).get_initial()
         if self.formfield1 is not None:
             initial['formfield1'] = Model.objects.get(pk=self.formfield1)
         return initial
 }}}

 In this case a primary key value is passed, but the problem also occurs
 when I pass a value for another field in the model.

 The working code in the form is:

 {{{
 formfield1 = forms.ModelChoiceField(queryset=Model.objects.all(),
 to_field_name='corresponding field name in the model', label='item to
 use', disabled=True)
 }}}

 If I leave out the disabled=True I can leave out to_field_name:

 {{{
 formfield1 = forms.ModelChoiceField(queryset=Model.objects.all(),
 label='item to use')
 }}}

 Then the field initialises properly, too.

 This however will fail:

 {{{
 formfield1 = forms.ModelChoiceField(queryset=Model.objects.all(),
 label='item to use', disabled=True)
 }}}

 The problem does not occur if I initialise the field via a custom __init__
 in the form itself. In that case I can leave out to_field_name:

 {{{
 selected_record = Model.objects.filter(some_flag=True).first()
 selected_field_item = selected_record.field1
 self.fields['formfield1'] = forms.CharField(max_length=64,  label='Field
 1', initial=selected_field_item, disabled=True)
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/30014#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/063.3f9b88e86a37b017142623140644f024%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to