我有一个Django应用程序,并希望在用户的配置文件中显示多个选择复选框。他们将能够选择多个项目。
这是我的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参数正在发送一个列表:
[u'choice_one', u'choice_two', u'choice_three']
所有表单字段显示正确,但是当我提交POST时,我收到错误
Error binding parameter 7 – probably unsupported type.
我需要在视图中进一步处理多选字段吗?模型字段类型是否正确?任何帮助或参考将不胜感激。