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

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

问题描述

我有一个Django应用程序,并希望在用户的个人资料中显示多个选择复选框。他们将能够选择多个项目。



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

 从profiles.choices导入SAMPLE_CHOICES 

类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:

 方法==POST:
profile_form = form_class(request.POST,instance = profile)
如果profile_form.is_valid():
...
profile.save
return render_to_response(template_name,{profile_form:profile_form,},context_instance = RequestContext(request))


$ b b

我可以看到POST只发送一个值:

  choice_field u'choice_three'

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

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

所有表单字段都显示正确,但当我提交POST时,会收到错误


错误绑定参数7 - 不支持的类型。


我需要在视图中进一步处理多选项字段吗?模型字段类型是否正确?非常感谢任何帮助或参考。

解决方案

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



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

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

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



然后,同步数据库并加载你想要的各种选项选项。



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

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

最后,视图:

  if request.method =='POST':
form = ProfileForm(request.POST)
if form.is_valid():
= 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))

应该提到你可以用几种不同的方式设置一个配置文件,包括继承。



祝你好运。


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.

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)

And my form class:

class ProfileForm(forms.ModelForm):
    choice_field = forms.MultipleChoiceField(choices=SAMPLE_CHOICES, widget=forms.CheckboxSelectMultiple)

    class Meta:
        model = Profile

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))

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

choice_field u'choice_three' 

And the local vars params is sending a list:

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

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

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.

解决方案

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.

Now, the ModelForm will build itself...

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

And finally, the view:

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.

Good luck.

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

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