我有一个 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
我的观点.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'
本地变量参数正在发送一个列表:
[u'choice_one', u'choice_two', u'choice_three']
所有表单字段都显示正确,但当我提交 POST 时,出现错误
绑定参数 7 时出错 - 可能是不受支持的类型。
我需要在视图中进一步处理多项选择字段吗?模型字段类型是否正确?任何帮助或参考将不胜感激。