我有一个Django应用程序,并希望在用户的配置文件中显示多个 Select 复选框.然后,他们将能够 Select 多个项目.
这是My Models.py的简化版本:
from profiles.choices import SAMPLE_CHOICES
class Profile(models.Model):
user = models.ForeignKey(User, unique=True, verbose_name_('user'))
choice_field = models.CharField(_('Some choices...'), choices=SAMPLE_CHOICES, max_length=50)
和我的窗体类:
class ProfileForm(forms.ModelForm):
choice_field = forms.MultipleChoiceField(choices=SAMPLE_CHOICES, widget=forms.CheckboxSelectMultiple)
class Meta:
model = Profile
和我的views.py:
if request.method == "POST":
profile_form = form_class(request.POST, instance=profile)
if profile_form.is_valid():
...
profile.save()
return render_to_response(template_name, {"profile_form": profile_form,}, context_instance=RequestContext(request))
我可以看到POST只发送了一个值:
choice_field u'choice_three'
本地的vars params正在发送一个列表:
[u'choice_one', u'choice_two', u'choice_three']
所有的表单域都显示正确,但是当我提交帖子时,我收到一个错误
绑定参数7时出错-可能是不支持的类型.
我是否需要在视图中进一步处理多项 Select 域?模型字段类型是否正确?任何帮助或推荐人都将不胜感激.