from django import newforms as forms from django.newforms.widgets import CheckboxSelectMultiple from django.utils.datastructures import MultiValueDict CommaSep = ',' class SelectCommaWidget(CheckboxSelectMultiple): def value_from_datadict(self, data, name): if isinstance(data, MultiValueDict): return data.getlist(name) retval = data.get(name) if isinstance(retval, type('')): retval = retval.split(CommaSep) return retval class MultipleChoiceCommaField(forms.MultipleChoiceField): """ MultipleChoiceCommaField will return the value sequence as a single string, comma-separated. This could easily generalized to a use a specified delimiter other than the comma. >>> Choices = (('1','almonds'), ('2','pecans'), ('3','cashews'), ('4','walnuts'), ('5','peanuts')) >>> class NutForm(forms.Form): ... nuts = MultipleChoiceCommaField(required=False, choices=Choices) ... >>> print NutForm({})