class CheckboxSelectMultiple
from django.forms import CheckboxSelectMultiple
Ancestors (MRO)
- CheckboxSelectMultiple
- ChoiceWidget
- Widget
Attributes
Defined in | |
---|---|
add_id_index = True
|
ChoiceWidget |
allow_multiple_selected = True
|
CheckboxSelectMultiple |
allow_multiple_selected = False
|
ChoiceWidget |
checked_attribute = {'checked': True}
|
ChoiceWidget |
input_type = 'checkbox'
|
CheckboxSelectMultiple |
input_type = None
|
ChoiceWidget |
is_localized = False
|
Widget |
is_required = False
|
Widget |
needs_multipart_form = False
|
Widget |
option_inherits_attrs = True
|
ChoiceWidget |
option_template_name = 'django/forms/widgets/checkbox_option.html'
|
CheckboxSelectMultiple |
option_template_name = None
|
ChoiceWidget |
supports_microseconds = True
|
Widget |
template_name = 'django/forms/widgets/checkbox_select.html'
|
CheckboxSelectMultiple |
template_name = None
|
ChoiceWidget |
Properties
def
is_hidden():
¶
Widget
def
media():
¶
CheckboxSelectMultiple
Getter
def _media(self):
# Get the media property of the superclass, if it exists
sup_cls = super(cls, self)
try:
base = sup_cls.media
except AttributeError:
base = Media()
# Get the media definition for this class
definition = getattr(cls, 'Media', None)
if definition:
extend = getattr(definition, 'extend', True)
if extend:
if extend is True:
m = base
else:
m = Media()
for medium in extend:
m = m + base[medium]
return m + Media(definition)
else:
return Media(definition)
else:
return base
def
media():
¶
ChoiceWidget
Getter
def _media(self):
# Get the media property of the superclass, if it exists
sup_cls = super(cls, self)
try:
base = sup_cls.media
except AttributeError:
base = Media()
# Get the media definition for this class
definition = getattr(cls, 'Media', None)
if definition:
extend = getattr(definition, 'extend', True)
if extend:
if extend is True:
m = base
else:
m = Media()
for medium in extend:
m = m + base[medium]
return m + Media(definition)
else:
return Media(definition)
else:
return base
def
media():
¶
Widget
Getter
def _media(self):
# Get the media property of the superclass, if it exists
sup_cls = super(cls, self)
try:
base = sup_cls.media
except AttributeError:
base = Media()
# Get the media definition for this class
definition = getattr(cls, 'Media', None)
if definition:
extend = getattr(definition, 'extend', True)
if extend:
if extend is True:
m = base
else:
m = Media()
for medium in extend:
m = m + base[medium]
return m + Media(definition)
else:
return Media(definition)
else:
return base
Methods
def
_format_value(*args, **kwargs):
¶
ChoiceWidget
def wrapped(*args, **kwargs):
warnings.warn(
"`%s.%s` is deprecated, use `%s` instead." %
(self.class_name, self.old_method_name, self.new_method_name),
self.deprecation_warning, 2)
return f(*args, **kwargs)
Widget
def wrapped(*args, **kwargs):
warnings.warn(
"`%s.%s` is deprecated, use `%s` instead." %
(self.class_name, self.old_method_name, self.new_method_name),
self.deprecation_warning, 2)
return f(*args, **kwargs)
def
_render(self, template_name, context, renderer=None):
¶
Widget
def _render(self, template_name, context, renderer=None):
if renderer is None:
renderer = get_default_renderer()
return mark_safe(renderer.render(template_name, context))
def
build_attrs(self, base_attrs, extra_attrs=None):
¶
Widget
Helper function for building an attribute dictionary.
def build_attrs(self, base_attrs, extra_attrs=None):
"Helper function for building an attribute dictionary."
attrs = base_attrs.copy()
if extra_attrs is not None:
attrs.update(extra_attrs)
return attrs
def
create_option(self, name, value, label, selected, index, subindex=None, attrs=None):
¶
ChoiceWidget
def create_option(self, name, value, label, selected, index, subindex=None, attrs=None):
index = str(index) if subindex is None else "%s_%s" % (index, subindex)
if attrs is None:
attrs = {}
option_attrs = self.build_attrs(self.attrs, attrs) if self.option_inherits_attrs else {}
if selected:
option_attrs.update(self.checked_attribute)
if 'id' in option_attrs:
option_attrs['id'] = self.id_for_label(option_attrs['id'], index)
return {
'name': name,
'value': value,
'label': label,
'selected': selected,
'index': index,
'attrs': option_attrs,
'type': self.input_type,
'template_name': self.option_template_name,
}
def
format_value(self, value):
¶
ChoiceWidget
Return selected values as a list.
def format_value(self, value):
"""Return selected values as a list."""
if not isinstance(value, (tuple, list)):
value = [value]
return [force_text(v) if v is not None else '' for v in value]
Widget
Return a value as it should appear when rendered in a template.
def format_value(self, value):
"""
Return a value as it should appear when rendered in a template.
"""
if value == '' or value is None:
return None
if self.is_localized:
return formats.localize_input(value)
return force_text(value)
def
get_context(self, name, value, attrs):
¶
ChoiceWidget
def get_context(self, name, value, attrs):
context = super(ChoiceWidget, self).get_context(name, value, attrs)
context['widget']['optgroups'] = self.optgroups(name, context['widget']['value'], attrs)
context['wrap_label'] = True
return context
Widget
def get_context(self, name, value, attrs):
context = {}
context['widget'] = {
'name': name,
'is_hidden': self.is_hidden,
'required': self.is_required,
'value': self.format_value(value),
'attrs': self.build_attrs(self.attrs, attrs),
'template_name': self.template_name,
}
return context
def
id_for_label(self, id_, index=None):
¶
CheckboxSelectMultiple
" Don't include for="field_0" in <label> because clicking such a label would toggle the first checkbox.
def id_for_label(self, id_, index=None):
""""
Don't include for="field_0" in <label> because clicking such a label
would toggle the first checkbox.
"""
if index is None:
return ''
return super(CheckboxSelectMultiple, self).id_for_label(id_, index)
ChoiceWidget
Use an incremented id for each option where the main widget references the zero index.
def id_for_label(self, id_, index='0'):
"""
Use an incremented id for each option where the main widget
references the zero index.
"""
if id_ and self.add_id_index:
id_ = '%s_%s' % (id_, index)
return id_
Widget
Returns the HTML ID attribute of this Widget for use by a <label>, given the ID of the field. Returns None if no ID is available. This hook is necessary because some widgets have multiple HTML elements and, thus, multiple IDs. In that case, this method should return an ID value that corresponds to the first ID in the widget's tags.
def id_for_label(self, id_):
"""
Returns the HTML ID attribute of this Widget for use by a <label>,
given the ID of the field. Returns None if no ID is available.
This hook is necessary because some widgets have multiple HTML
elements and, thus, multiple IDs. In that case, this method should
return an ID value that corresponds to the first ID in the widget's
tags.
"""
return id_
def
optgroups(self, name, value, attrs=None):
¶
ChoiceWidget
Return a list of optgroups for this widget.
def optgroups(self, name, value, attrs=None):
"""Return a list of optgroups for this widget."""
groups = []
has_selected = False
for index, (option_value, option_label) in enumerate(chain(self.choices)):
if option_value is None:
option_value = ''
subgroup = []
if isinstance(option_label, (list, tuple)):
group_name = option_value
subindex = 0
choices = option_label
else:
group_name = None
subindex = None
choices = [(option_value, option_label)]
groups.append((group_name, subgroup, index))
for subvalue, sublabel in choices:
selected = (
force_text(subvalue) in value and
(has_selected is False or self.allow_multiple_selected)
)
if selected is True and has_selected is False:
has_selected = True
subgroup.append(self.create_option(
name, subvalue, sublabel, selected, index,
subindex=subindex, attrs=attrs,
))
if subindex is not None:
subindex += 1
return groups
def
options(self, name, value, attrs=None):
¶
ChoiceWidget
Yield a flat list of options for this widgets.
def options(self, name, value, attrs=None):
"""Yield a flat list of options for this widgets."""
for group in self.optgroups(name, value, attrs):
for option in group[1]:
yield option
def
render(self, name, value, attrs=None, renderer=None):
¶
Widget
Returns this Widget rendered as HTML, as a Unicode string.
def render(self, name, value, attrs=None, renderer=None):
"""
Returns this Widget rendered as HTML, as a Unicode string.
"""
context = self.get_context(name, value, attrs)
return self._render(self.template_name, context, renderer)
def
subwidgets(self, name, value, attrs=None):
¶
ChoiceWidget
Yield all "subwidgets" of this widget. Used to enable iterating options from a BoundField for choice widgets.
def subwidgets(self, name, value, attrs=None):
"""
Yield all "subwidgets" of this widget. Used to enable iterating
options from a BoundField for choice widgets.
"""
value = self.format_value(value)
for option in self.options(name, value, attrs):
yield option
Widget
def subwidgets(self, name, value, attrs=None):
context = self.get_context(name, value, attrs)
yield context['widget']
def
use_required_attribute(self, initial):
¶
CheckboxSelectMultiple
def use_required_attribute(self, initial):
# Don't use the 'required' attribute because browser validation would
# require all checkboxes to be checked instead of at least one.
return False
Widget
def use_required_attribute(self, initial):
return not self.is_hidden
def
value_from_datadict(self, data, files, name):
¶
ChoiceWidget
def value_from_datadict(self, data, files, name):
getter = data.get
if self.allow_multiple_selected:
try:
getter = data.getlist
except AttributeError:
pass
return getter(name)
Widget
Given a dictionary of data and this widget's name, returns the value of this widget. Returns None if it's not provided.
def value_from_datadict(self, data, files, name):
"""
Given a dictionary of data and this widget's name, returns the value
of this widget. Returns None if it's not provided.
"""
return data.get(name)
def
value_omitted_from_data(self, data, files, name):
¶
CheckboxSelectMultiple
def value_omitted_from_data(self, data, files, name):
# HTML checkboxes don't appear in POST data if not checked, so it's
# never known if the value is actually omitted.
return False
Widget
def value_omitted_from_data(self, data, files, name):
return name not in data