本文整理汇总了Python中django.forms.widgets.Select方法的典型用法代码示例。如果您正苦于以下问题:Python widgets.Select方法的具体用法?Python widgets.Select怎么用?Python widgets.Select使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.forms.widgets
的用法示例。
在下文中一共展示了widgets.Select方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from django.forms import widgets [as 别名]
# 或者: from django.forms.widgets import Select [as 别名]
def __init__(self, attrs=None, choices=(), default_unit=None):
"""
Split the field in 2 widgets:
- the first widget is a positive integer input,
- the second widget is a select box to choose a pre-defined time unit (minutes, hours,
days, weeks or months),
e.g: 3 hours is split in: 3 (integer input) | hour (select)
"""
self.default_unit = default_unit
super().__init__(
(
widgets.NumberInput({**(attrs or {}), "min": 0}),
widgets.Select(attrs, choices),
)
)
示例2: test_raw_id_fields_widget_override
# 需要导入模块: from django.forms import widgets [as 别名]
# 或者: from django.forms.widgets import Select [as 别名]
def test_raw_id_fields_widget_override(self):
"""
The autocomplete_fields, raw_id_fields, and radio_fields widgets may
overridden by specifying a widget in get_formset().
"""
class ConcertInline(TabularInline):
model = Concert
fk_name = 'main_band'
raw_id_fields = ('opening_band',)
def get_formset(self, request, obj=None, **kwargs):
kwargs['widgets'] = {'opening_band': Select}
return super().get_formset(request, obj, **kwargs)
class BandAdmin(ModelAdmin):
inlines = [ConcertInline]
ma = BandAdmin(Band, self.site)
band_widget = list(ma.get_formsets_with_inlines(request))[0][0]().forms[0].fields['opening_band'].widget
# Without the override this would be ForeignKeyRawIdWidget.
self.assertIsInstance(band_widget, Select)
示例3: __init__
# 需要导入模块: from django.forms import widgets [as 别名]
# 或者: from django.forms.widgets import Select [as 别名]
def __init__(self, *args, **kwargs):
super(BootsrapForm, self).__init__(*args, **kwargs)
for field in self.fields.values():
# Only tweak the field if it will be displayed
if not isinstance(field.widget, widgets.HiddenInput):
attrs = {}
if (
isinstance(field.widget, (widgets.Input, widgets.Select, widgets.Textarea)) and
not isinstance(field.widget, (widgets.CheckboxInput,))
):
attrs['class'] = "form-control"
if isinstance(field.widget, (widgets.Input, widgets.Textarea)) and field.label:
attrs["placeholder"] = field.label
if field.required:
attrs["required"] = "required"
field.widget.attrs.update(attrs)
示例4: create_select
# 需要导入模块: from django.forms import widgets [as 别名]
# 或者: from django.forms.widgets import Select [as 别名]
def create_select(self, name, field, value, val, choices, none_value):
if 'id' in self.attrs:
id_ = self.attrs['id']
else:
id_ = 'id_%s' % name
if not self.is_required:
choices.insert(0, none_value)
local_attrs = self.build_attrs(id=field % id_)
s = Select(choices=choices)
select_html = s.render(field % name, val, local_attrs)
return select_html
示例5: __init__
# 需要导入模块: from django.forms import widgets [as 别名]
# 或者: from django.forms.widgets import Select [as 别名]
def __init__(
self,
attrs=None,
choices=(),
default_effort_unit=None,
default_reference_unit=None,
):
"""
Split the field in 3 widgets:
- the first widget is a positive integer input,
- the second widget is a select box to choose a pre-defined time unit (minutes, hours,
days, weeks or months),
- the third widget is a select box to choose the pre-defined time unit of reference.
e.g: 3 hours/day is split in: 3 (integer input) | hour (select) | day (select)
"""
self.default_effort_unit = default_effort_unit
self.default_reference_unit = default_reference_unit
super().__init__(
(
widgets.NumberInput({**(attrs or {}), "min": 0}),
# Remove the last choice: it can never be chosen as it must be strictly smaller
# than the reference time unit
widgets.Select(attrs, choices[:-1]),
# Remove the first choice: it can never be chosen as it must be strictly greater
# than the effort time unit
widgets.Select(attrs, choices[1:]),
)
)
示例6: create_select
# 需要导入模块: from django.forms import widgets [as 别名]
# 或者: from django.forms.widgets import Select [as 别名]
def create_select(self, name, field, value, val, choices):
if 'id' in self.attrs:
id_ = self.attrs['id']
else:
id_ = 'id_%s' % name
if not (self.required and val):
choices.insert(0, self.none_value)
local_attrs = self.build_attrs(id=field % id_)
s = Select(choices=choices)
select_html = s.render(field % name, val, local_attrs)
return select_html
示例7: dropdown
# 需要导入模块: from django.forms import widgets [as 别名]
# 或者: from django.forms.widgets import Select [as 别名]
def dropdown(cls, choice):
attrs = {
"class": "extra-widget extra-widget-dropdown",
"style": "display: none;",
}
return ChoiceField(
required=False,
choices=options_as_choices(choice),
widget=Select(attrs=attrs),
)
示例8: __init__
# 需要导入模块: from django.forms import widgets [as 别名]
# 或者: from django.forms.widgets import Select [as 别名]
def __init__(self, attrs=None):
_widgets = (
TinyMCE(attrs=attrs, mce_attrs=MCE_ATTRIBUTES_SHORT),
widgets.Select(attrs=attrs, choices=RATE_CHOICES),
)
super().__init__(_widgets, attrs)
示例9: test_default_foreign_key_widget
# 需要导入模块: from django.forms import widgets [as 别名]
# 或者: from django.forms.widgets import Select [as 别名]
def test_default_foreign_key_widget(self):
# First, without any radio_fields specified, the widgets for ForeignKey
# and fields with choices specified ought to be a basic Select widget.
# ForeignKey widgets in the admin are wrapped with RelatedFieldWidgetWrapper so
# they need to be handled properly when type checking. For Select fields, all of
# the choices lists have a first entry of dashes.
cma = ModelAdmin(Concert, self.site)
cmafa = cma.get_form(request)
self.assertEqual(type(cmafa.base_fields['main_band'].widget.widget), Select)
self.assertEqual(
list(cmafa.base_fields['main_band'].widget.choices),
[('', '---------'), (self.band.id, 'The Doors')])
self.assertEqual(type(cmafa.base_fields['opening_band'].widget.widget), Select)
self.assertEqual(
list(cmafa.base_fields['opening_band'].widget.choices),
[('', '---------'), (self.band.id, 'The Doors')]
)
self.assertEqual(type(cmafa.base_fields['day'].widget), Select)
self.assertEqual(
list(cmafa.base_fields['day'].widget.choices),
[('', '---------'), (1, 'Fri'), (2, 'Sat')]
)
self.assertEqual(type(cmafa.base_fields['transport'].widget), Select)
self.assertEqual(
list(cmafa.base_fields['transport'].widget.choices),
[('', '---------'), (1, 'Plane'), (2, 'Train'), (3, 'Bus')])
示例10: __init__
# 需要导入模块: from django.forms import widgets [as 别名]
# 或者: from django.forms.widgets import Select [as 别名]
def __init__(self, attrs=None):
# create choices for days, months, years
_attrs = attrs or {} # default class
_attrs['class'] = (_attrs.get('class', '') + ' w-month-year').strip()
_widgets = [widgets.Select(attrs=_attrs, choices=MONTHS.items())]
_attrs['class'] += " w-year"
_widgets.append(widgets.NumberInput(attrs=_attrs))
super(MonthSelectorWidget, self).__init__(_widgets, attrs)