Django 多项选择字段/复选框选择多项 [英] Django Multiple Choice Field / Checkbox Select Multiple

查看:45
本文介绍了Django 多项选择字段/复选框选择多项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Django 应用程序,想在用户的个人资料中显示多选复选框.然后他们将能够选择多个项目.

I have a Django application and want to display multiple choice checkboxes in a user's profile. They will then be able to select multiple items.

这是我的models.py的简化版本:

This is a simplified version of 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:

And my 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 只发送一个值:

I can see that the POST is only sending one value:

choice_field u'choice_three' 

并且本地 vars params 正在发送一个列表:

And the local vars params is sending a list:

[u'choice_one', u'choice_two', u'choice_three']

所有表单字段都显示正确,但是当我提交 POST 时,出现错误

All of the form fields display correct, but when I submit a POST, I get an error

错误绑定参数 7 - 可能是不受支持的类型.

Error binding parameter 7 - probably unsupported type.

我是否需要在视图中进一步处理多项选择字段?模型字段类型是否正确?任何帮助或参考将不胜感激.

Do I need to process the multiple choice field further in the view? Is the model field type correct? Any help or references would be greatly appreciated.

推荐答案

配置文件选择需要设置为 ManyToManyField 才能正常工作.

The profile choices need to be setup as a ManyToManyField for this to work correctly.

所以...你的模型应该是这样的:

So... your model should be like this:

class Choices(models.Model):
  description = models.CharField(max_length=300)

class Profile(models.Model):
  user = models.ForeignKey(User, blank=True, unique=True, verbose_name='user')
  choices = models.ManyToManyField(Choices)

然后,同步数据库并加载带有您想要的各种可用选项的选项.

Then, sync the database and load up Choices with the various options you want available.

现在,ModelForm 将自行构建...

Now, the ModelForm will build itself...

class ProfileForm(forms.ModelForm):
  Meta:
    model = Profile
    exclude = ['user']

最后,视图:

if request.method=='POST':
  form = ProfileForm(request.POST)
  if form.is_valid():
    profile = form.save(commit=False)
    profile.user = request.user
    profile.save()
else:
  form = ProfileForm()

return render_to_response(template_name, {"profile_form": form}, context_instance=RequestContext(request))

值得一提的是,您可以通过多种不同的方式设置配置文件,包括继承.也就是说,这也适用于您.

It should be mentioned that you could setup a profile in a couple different ways, including inheritance. That said, this should work for you as well.

祝你好运.

这篇关于Django 多项选择字段/复选框选择多项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆