python - Django MultipleChoiceField with CheckboxSelectMultiple error if only one item selected -
python - Django MultipleChoiceField with CheckboxSelectMultiple error if only one item selected -
i have django form described in title. has set of check boxes. if 2 or more selected works perfect. if 1 item selected "errorlist: come in list of values."
i have examined self.request.post['key'] , notice list contains multiple entries of same key (different value) when select multiple choices. contains 1 entry 1 selection (makes sense). in either case these entries string, not unicode string or list.
is error related multiplechoicefield?
code:
# getallchoices() returns list([u'key', u'value'], ...) class testmodelform(forms.form): choices = multiplechoicefield(label='test choices', choices=getallchoices(), help_text="testing help text", required=false, widget=checkboxselectmultiple())
update:
in post handler form if add together sec item manually works again. thinking multiplechoicefield demands >1 items cannot find documented anywhere , if makes no sense.
the next 'hack' fixes appending duplicate item, not acceptable solution however.
tmplist = self.request.post.getall('installed_apps') if len(tmplist) == 1: self.request.post.add('installed_apps', tmplist[0])
from django source code:
def clean(self, value): if self.required , not value: raise validationerror(self.error_messages['required'], code='required') elif not self.required , not value: homecoming self.queryset.none() #this line culprit if not isinstance(value, (list, tuple)): raise validationerror(self.error_messages['list'], code='list')
this raises error indicated "list"
should 'enter list of values.'
you're seeing. seems in fact expect list of values, , 1 cleaned value not list. 1 effort create additional, blank element in choices, , have selected default, , hidden in form. way, long select @ to the lowest degree 1 element, post contain 2 (the chosen , hidden), , validate.
otherwise, maybe different form element best bet.
hope helps!
python django forms
Comments
Post a Comment