views:

32

answers:

1

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.

+3  A: 

You have to tell the modelformset_factory to use your customized FeedbackForm:

FeedbackFormSet = modelformset_factory(Feedback, form=FeedbackForm, extra=1)

After that, one of your three approaches should work (I'd prefer the second one for Django 1.2).

piquadrat
Facepalm. Thank you.And FWIW there is a bug with formset_factory methods that means method #2 above doesn't work. http://code.djangoproject.com/ticket/13095 But method #1 works just fine. :)
pfctdayelise