Django Multiple Choice Field / Checkbox Select Multiple Django Multiple Choice Field / Checkbox Select Multiple python python

Django Multiple Choice Field / Checkbox Select Multiple


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.


Brant's solution is absolutely correct, but I needed to modify it to make it work with multiple select checkboxes and commit=false. Here is my solution:

models.py

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'))   the_choices = models.ManyToManyField(Choices)

forms.py

class ProfileForm(forms.ModelForm):    the_choices = forms.ModelMultipleChoiceField(queryset=Choices.objects.all(), required=False, widget=forms.CheckboxSelectMultiple)    class Meta:        model = Profile        exclude = ['user']

views.py

if request.method=='POST':    form = ProfileForm(request.POST)    if form.is_valid():        profile = form.save(commit=False)        profile.user = request.user        profile.save()        form.save_m2m() # needed since using commit=False    else:        form = ProfileForm()return render_to_response(template_name, {"profile_form": form}, context_instance=RequestContext(request))


The models.CharField is a CharField representation of one of the choices. What you want is a set of choices. This doesn't seem to be implemented in django (yet).

You could use a many to many field for it, but that has the disadvantage that the choices have to be put in a database. If you want to use hard coded choices, this is probably not what you want.

There is a django snippet at http://djangosnippets.org/snippets/1200/ that does seem to solve your problem, by implementing a ModelField MultipleChoiceField.