views:

277

answers:

1

Hi,

I'm building an application on app Engine and I want to make a form field with multiple choices.
Here is my form (it uses django.newforms from the app engine sdk (django 0.96)) :

from google.appengine.ext.db import djangoforms
from django import newforms

class KeywordForm(djangoforms.ModelForm):
    class Meta:
        model = Keyword
        exclude = ['site', 'created_at', 'last_update']

    choices = [ (1, 'value1'), (2, 'value2'), (3, 'value3'), (4, 'value4') ]
    server = newforms.fields.MultipleChoiceField(choices = choices)

The problem is : when I submit the form (with one or more values selected) I've this validation error : "Enter a list of values."

I don't understand why... some help on this problem will be very appreciated.
Thanks ! :)

Edit (extra informations) :
Here is the form validation code :

 form = forms.KeywordForm(data=self.request.POST)
 if form.is_valid():
     ...

self.request.POST :

UnicodeMultiDict([(u'keyword', u'test'), (u'server[]', u'1'), (u'server[]', u'2')])
+1  A: 

I've found a solution !

The problem is self.request.POST dictionnary provided to my form's constructor.
It's format is not appreciated by MultipleChoiceField.clean() function, so I transformed it.

Here is the working validation code :

 args = self.request.arguments()
 data = {}
 for i in args:
     data[i] = self.request.get_all(i)
 form = forms.KeywordForm(data=data)
 if form.is_valid():
     [...]
Mat
You can simply pass self.request.POST.items().
Nick Johnson
Thanks a lot Nick, it's a lot more elegant !
Mat