我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用django.forms.SelectMultiple()。
def init_chosen_widget(field_items, disable_help_text=True): """ Add ``.chosen-widget`` html class attribute to all fields of type ``Select`` or ``SelectMultiple``. :param field_items: Django field items :param bool disable_help_text: Disable the fields help text. Default: True """ for name, field in field_items: if isinstance(field.widget, SelectMultiple) or isinstance(field.widget, Select): field.widget.attrs['class'] = 'chosen-widget' # field.widget.attrs['style'] = 'width: 100%;' if disable_help_text: field.help_text = None
def __init__(self, world, sector, *args, **kwargs): super(attackform, self).__init__(*args, **kwargs) query = world.controlled_fleets.all().exclude(sector='hangar').exclude(sector='warping') query = query.filter(sector=sector) exclude_list = [] for f in query: if f.power() == 0 or f.powermodifiers() == 0: exclude_list.append(f.pk) for pk in exclude_list: query = query.exclude(pk=pk) self.fields['fleets'] = forms.ModelMultipleChoiceField(queryset=query, widget=forms.SelectMultiple( attrs={'style': 'color: black; background-color: white; min-width: 80px;'}))
def value_from_datadict(self, data, files, name): from django.utils.datastructures import MultiValueDict if isinstance(data, MultiValueDict): # Normally, we'd want a list here, which is what we get from the # SelectMultiple superclass, but the SimpleArrayField expects to # get a delimited string, so we're doing a little extra work. return self.delimiter.join(data.getlist(name)) return data.get(name, None)
def value_from_datadict(self, data, files, name): if isinstance(data, MultiValueDict): # Normally, we'd want a list here, which is what we get from the # SelectMultiple superclass, but the SimpleArrayField expects to # get a delimited string, so we're doing a little extra work. array = self.delimiter.join(data.getlist(name)) return array return data.get(name, None)
def __init__(self, null_option=None, *args, **kwargs): self.null_option = null_option if 'required' not in kwargs: kwargs['required'] = False if 'widget' not in kwargs: kwargs['widget'] = forms.SelectMultiple(attrs={'size': 6}) super(FilterChoiceFieldMixin, self).__init__(*args, **kwargs)
def __init__(self, *args, **kwargs): super(DualListField, self).__init__(*args, **kwargs) self.widget = SelectMultiple(attrs={'class': 'duallistbox'})