Python django.forms.widgets.Select() Examples

The following are 11 code examples of django.forms.widgets.Select(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.forms.widgets , or try the search function .
Example #1
Source File: duration.py    From richie with MIT License 7 votes vote down vote up
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),
            )
        ) 
Example #2
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 6 votes vote down vote up
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) 
Example #3
Source File: forms.py    From django-cas-server with GNU General Public License v3.0 6 votes vote down vote up
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) 
Example #4
Source File: widgets.py    From django-monthfield with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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) 
Example #5
Source File: widgets.py    From GTDWeb with GNU General Public License v2.0 5 votes vote down vote up
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 
Example #6
Source File: effort.py    From richie with MIT License 5 votes vote down vote up
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:]),
            )
        ) 
Example #7
Source File: widgets.py    From luscan-devel with GNU General Public License v2.0 5 votes vote down vote up
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 
Example #8
Source File: widgets.py    From callisto-core with GNU Affero General Public License v3.0 5 votes vote down vote up
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),
        ) 
Example #9
Source File: fields.py    From hypha with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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) 
Example #10
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
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')]) 
Example #11
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
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')])