I have a ManyToManyField that I want to present in a form, as a CheckboxSelectMultiple widget. Why don't any of these methods work? (See Attempt #1, #2 and #3 below.) According to everything I've read in the docs and on SO, at least one of them should work. But I still have a stubborn SelectMultiple widget that refuses to budge.
from django.forms.widgets import CheckboxSelectMultiple
from django.db import models
from django import forms
KEYWORD_CHOICES = (('sky', 'sky'),('wind','wind'),)
class Keyword(models.Model):
keyword = models.CharField(max_length=50)
def __unicode__(self):
return self.keyword
class Feedback(models.Model):
summary = models.CharField(max_length=200)
keys = models.ManyToManyField(Keyword, blank=True, null=True)
###################################
class FeedbackForm(forms.ModelForm):
# attempt 1
# based on
# http://docs.djangoproject.com/en/dev/ref/forms/widgets/#specifying-widgets
keys = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple,
choices=KEYWORD_CHOICES)
class Meta:
model = Feedback
# attempt 2
# based on
# http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#overriding-the-default-field-types-or-widgets
widgets = {
'keys':CheckboxSelectMultiple(),
}
# attempt 3
# based on
# bitkickers.blogspot.com/2010/05/django-manytomanyfield-on-modelform-as.html
def __init__(self, *args, **kwargs):
super(FeedbackForm, self).__init__(*args, **kwargs)
self.fields["keys"].widget = CheckboxSelectMultiple()
self.fields["keys"].queryset = Keywords.objects.all()
I used a fixture to populate my initial Keyword instances.
I've also tried to override the help_text without success.
In my views.py
I am using a formset:
from app.models import *
from django.forms.models import modelformset_factory
def submit(request):
FeedbackFormSet = modelformset_factory(Feedback, extra=1)
#[...]
feedbackforms = FeedbackFormSet(prefix='feedback',queryset=Feedback.objects.none())
But I don't see how that would make a difference.
I am using Django version 1.2.1.