[Django] #27578: ModelChoiceField.to_python() somehow receives value as type 'list'

7 views
Skip to first unread message

Django

unread,
Dec 6, 2016, 5:11:13 PM12/6/16
to django-...@googlegroups.com
#27578: ModelChoiceField.to_python() somehow receives value as type 'list'
--------------------------------------------+------------------------
Reporter: Julian Schneider | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 1.9
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
--------------------------------------------+------------------------
Whenever im running form.is_valid() i get the error:

> Select a valid choice. That choice is not one of the available
> choices.

Here is what I do in my view:

{{{

timeframes = HostTimeFrame.objects.all()
if request.method == 'POST':
form = SelectDatesForm(request.POST, timeframes=timeframes)
if form.is_valid():
pass
else:
form = SelectDatesForm(timeframes=timeframes)

}}}

My form does this:


{{{
class SelectDatesForm(forms.Form):
timeframes =
forms.ModelChoiceField(queryset=HostTimeFrame.objects.none(),
widget=forms.CheckboxSelectMultiple,
empty_label=None)
def __init__(self, *args, **kwargs):
qs = kwargs.pop('timeframes')
super(SelectDatesForm, self).__init__(*args, **kwargs)
self.fields['timeframes'].queryset = qs.order_by('start')
}}}


Ive been trying for hours to find where this actual validation is done,
and realized the exception is raised in ModelChoiceField.to_python()


{{{
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')
}}}

Where value is the primary key, ** but in List format**, this raises and
exception.

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

Django

unread,
Dec 6, 2016, 5:13:45 PM12/6/16
to django-...@googlegroups.com
#27578: ModelChoiceField.to_python() somehow receives value as type 'list'
----------------------------------+--------------------------------------

Reporter: Julian Schneider | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 1.9
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+--------------------------------------
Description changed by Julian Schneider:

Old description:

New description:

{{{

}}}

My form does this:

exception.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/27578#comment:1>

Django

unread,
Dec 6, 2016, 5:27:34 PM12/6/16
to django-...@googlegroups.com
#27578: ModelChoiceField.to_python() somehow receives value as type 'list'
----------------------------------+--------------------------------------

Reporter: Julian Schneider | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 1.9
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+--------------------------------------

Comment (by Julian Schneider):

Replying to [comment:1 Julian Schneider]:
Created my own bugfixes in a custom field:

{{{
class CustomModelChoiceField(ModelChoiceField):
def to_python(self, value):
if value in self.empty_values:
return None


try:
key = self.to_field_name or 'pk'

if type(value) == list:
value = value[0]


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/27578#comment:2>

Django

unread,
Dec 6, 2016, 10:22:19 PM12/6/16
to django-...@googlegroups.com
#27578: ModelChoiceField.to_python() somehow receives value as type 'list'
----------------------------------+--------------------------------------

Reporter: Julian Schneider | Owner: nobody
Type: Bug | Status: closed
Component: Forms | Version: 1.9
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+--------------------------------------
Changes (by Tim Graham):

* status: new => closed
* resolution: => invalid


Comment:

If you're using `widget=forms.CheckboxSelectMultiple` then you need to use
`ModelMultipleChoiceField` which accepts a list of values.

--
Ticket URL: <https://code.djangoproject.com/ticket/27578#comment:3>

Reply all
Reply to author
Forward
0 new messages