﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
31020	Disabled ModelChoiceField produces an error in to_python	John Supplee	nobody	"When a form with a disabled `ModelChoiceField` with an initial value is validated it produces an error.

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
}}}"	Bug	closed	Forms	2.2	Normal	duplicate			Unreviewed	0	0	0	0	0	0
