It seems that disabled fields go through the data cleaning process and the
`.to_python(value)` method is called. For most fields that is not a
problem as the value passed to the `to_python()` method is massaged and
returned. In the case of a `ModelChoiceField` the initial value passed is
a `Model` object. This causes `to_python()` to throw a `ValidationError`
of:
Select a valid choice. That choice is not one of the available
choices.
It seems that `to_python()` is expecting a text value from a POST.
`ModelChoiceField.to_python()` needs to be modified to return the passed
value if the field is disabled.
The below code is from 2.2.7 with the addition of two new lines at the
beginning of the method.
{{{
def to_python(self, value):
if self.disabled:
return value
if value in self.empty_values:
return None
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/31020>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* status: new => closed
* resolution: => duplicate
Comment:
Duplicate of #30014. (Fixed in Django 3.0.)
--
Ticket URL: <https://code.djangoproject.com/ticket/31020#comment:1>
Comment (by John Supplee):
Why can't this bug be fixed in Django 2?
--
Ticket URL: <https://code.djangoproject.com/ticket/31020#comment:2>
Comment (by Carlton Gibson):
[https://docs.djangoproject.com/en/dev/internals/release-process
/#supported-versions-policy It doesn't qualify for a backport under our
Supported Versions Policy]. Django 3.0 will be released on Dec 2.
--
Ticket URL: <https://code.djangoproject.com/ticket/31020#comment:3>