【发布时间】:2011-02-13 03:57:15
【问题描述】:
我有一个 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 时,我收到错误
错误绑定参数 7 - 可能是不受支持的类型。
我是否需要在视图中进一步处理多项选择字段?模型字段类型是否正确?任何帮助或参考将不胜感激。
【问题讨论】:
-
您能否针对 POST 时遇到的错误发布完整的堆栈跟踪信息?
标签: python django select checkbox