开发者

MultipleChoiceField with SelectMultiple widget "Value [...] not a valid choice."

I have a field defined in my model-

    languages = models.CharField(max_length = 30, choices=LANGUAGE_CHOICES, blank = True, null = T开发者_如何学编程rue)

The choices are simple-

LANGUAGE_CHOICES = (
    ('English', 'English'),
)

I define a ModelForm on this model and override the field-

languages = forms.MultipleChoiceField(choices=LANGUAGE_CHOICES, widget=forms.SelectMultiple)

When I fill out the form, select "English", and try to submit, I get an error-

languages

Value u"[u'English']" is not a valid choice

Is there something basic that I am doing wrong? Or is the MultipleChoiceField combined with the SelectMultiple widget not the way to go?

Also, is there any reason why the choices tuple can't have the same value twice, like I have it now ('English', 'English')?

Here is some additional code that might be useful in getting to the bottom of this

Template Code:

<div class="abovepad">
<label for="id_languages">Languages:</label>
    {{form.languages}}
</div>

The portion of POST data with the languages:

u'languages': [u'English'],

Relevant snippet of ModelForm code:

class ProfileForm(forms.ModelForm):        
languages = forms.MultipleChoiceField(choices=LANGUAGE_CHOICES)
class Meta:
    model = Student

As you see, I barely customized anything. In the past when I ran into this issue I would switch to making the items in the list to models and then using ManyToMany fields which did not cause the same issue. For those instances, having the items be models made sense; in this case it doesn't. I just want to know whether I'm doing something wrong or whether this combo is not supposed to work. If there's no real answer then my other option would be to try and dig through the Django form code to see why its doing what its doing.


You must remove choices=LANGUAGE_CHOICES from your model definition: what your form return is the list of selected value, that doesn't exist in your models.LANGUAGE_CHOICES


I know this over a year old now but thought it may still be helpful to attempt an answer.

I believe there may be a couple of issues here, but the main issue at hand is that the ModelForm is expecting a key to the database record not a string value.

I would suggest making a Form instead of ModelForm, if you are making a form that isn't relating to your database. But if you do want to interact with your database then use the ModelForm and and define a ForeignKey in your Student model to relate to the language table, letting the ModelForm do the work for you.

Example app based on my assumptions of your model.

your_project/example_app/models.py:

from django.db import models

class Student(models.Model):
    name = models.CharField(max_length=255)
    language = models.ForeignKey('Language')

class Language(models.Model):
    name = models.CharField(max_length=255)

your_project/example_app/forms.py:

from django.forms import ModelForm
from example.models import Student

class StudentForm(ModelForm):
    class Meta:
        model = Student

Relevant docs: https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#modelform

snippet:

As you might expect, the ForeignKey and ManyToManyField model field types are special cases:

ForeignKey is represented by django.forms.ModelChoiceField, which is a ChoiceField whose choices are a model QuerySet. ManyToManyField is represented by django.forms.ModelMultipleChoiceField, which is a MultipleChoiceField whose choices are a model QuerySet.


The error message suggests that instead of containing an array, languages contains a unicode string containing a representation of that array. languages should evaluate to [u'English'] (array) but instead evaluates to u"[u'English']" (unicode string).

Does your ModelForm include any custom validation (say, a clean_languages function) that might be changing the value for languages?

BTW: SelectMultiple is already the default widget for MultipleChoiceField so there's no need to specify the widget.

Some things that would be useful for finding the cause:

  • the template code that renders the form
  • the POST data returned by the form
  • the code used for your ModelForm class
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜