from itertools import chain from django.forms.widgets import Select, CheckboxSelectMultiple, CheckboxInput, mark_safe from django.utils.encoding import force_unicode from django.utils.html import escape, conditional_escape class CheckboxSelectMultipleWithDisabled(CheckboxSelectMultiple): """ Subclass of Django's checkbox select multiple widget that allows disabling checkbox-options. To disable an option, pass a dict instead of a string for its label, of the form: {'label': 'option label', 'disabled': True} """ def render(self, name, value, attrs=None, choices=()): if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name) output = [u'') return mark_safe(u'\n'.join(output))