#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:  2.1
     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
-------------------------------------+-------------------------------------
Changes (by Etienne Chove):

 * status:  closed => new
 * cc: Etienne Chove (added)
 * version:  1.11 => 2.1
 * resolution:  needsinfo =>


Comment:

 Here is the test example I wrote in
 {{{tests/forms_tests/field_tests/test_modelchoicefield.py}}} reproducing
 this bug:

 {{{
 from django.forms import ModelChoiceField, Form
 from django.test import TestCase

 from ..models import ChoiceOptionModel


 class ModelChoiceFieldTest(TestCase):

     def test_disabled_field_1(self):
         opt1 = ChoiceOptionModel(12345)
         opt1.save()
         class MyForm(Form):
             field = ModelChoiceField(
                 queryset=ChoiceOptionModel.objects.all(), disabled=True,
                 initial=opt1)
         self.assertTrue(MyForm({}).is_valid())
 }}}

 When {{{disabled}}} is {{{True}}}, the function {{{to_python}}} is called
 with initial value already having model type.

 Here's new version of function {{{ModelChoiceField.to_python}}} in
 {{{django/forms/models.py}}} :

 {{{
 =    def to_python(self, value):
 =        if value in self.empty_values:
 =             return None
 +         if type(value) is self.queryset.model:
 +             return value
 =         try:
 =             key = self.to_field_name or 'pk'
 =             value = self.queryset.get(**{key: value})
 =         except (ValueError, TypeError,
 self.queryset.model.DoesNotExist):
 =             raise ValidationError(self.error_messages['invalid_choice'],
 code='invalid_choice')
 =         return value
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/30014#comment:7>
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.f5315bfe8b95bec2fa8f32a7dd32a7ac%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to