SelectMultiple Django Form Fields

Listen as audio
SelectMultiple Django Form Fields

Hello Techies,

In this blog, we will check different SelectMultiple Django form fields(MultipleChoiceField, TypedMultipleChoiceField) with examples.

Form fields themselves are classes; They manage form data and perform validation when the form is submitted.

Syntax : FieldType(**kwargs) 

Different types of SelectMultiple Django Form Fields

MultipleChoiceField

The default widget for this input is the SelectMultiple. The empty value is an empty list. It Normalizes to a list of strings. It Validates that each value in the given list of values ​​exists in the list of choices.

It takes one extra required argument which is choices.

Syntax: MultipleChoiceField(**kwargs)

Example:

from django import forms

class SelectMultipleForm(forms.Form):
        multiple_choice_field = forms.MultipleChoiceField(label='Multiple Choice Field', label_suffix=" : ",
                                  required=True, disabled=False, 
                                  widget=forms.SelectMultiple(attrs={'class': 'form-control'}), choices=city,
                                  error_messages={'required': "This field is required."})

Let’s run the server and check the result.

Rendered HTML

Explanation:

Field OptionsDescription
labelThis argument allows you to specify a “Field Caption” label for this area.
label_suffixThis argument allows you to insert some text after the field’s label.
choicesAn iterable of 2-tuples to use as choices for choices field or callable such as an iterable.
requiredBy default, each field assumes a value as required, so you need to set the required field as a false to make it not necessary.
widgetThis Argument lets you specify widget classes to use when presenting this field. So you can use bootstrap class or any other CSS class to decorate your field. Also, you can pass the choice list.
For eg. multiple_choice_field = forms.MultipleChoiceField(widget=forms.SelectMultiple(attrs={‘class’: ‘form-control’}))
error_messagesThis argument allows you to overwrite the default error message that the field will raise. Pass the error message to overwrite in the dictionary with the matching key.
disabledDisabled Boolean Argument, if set to true, disables the form field using the disabled HTML attribute so that it cannot be edited by users.

TypedMultipleChoiceField

Same as a MultipleChoiceField, except this field takes two extra arguments, coerce and empty_value.

The default widget for this input is the SelectMultiple. The empty value is whatever you’ve given as empty_value. It Normalizes to a list of values of the type provided by the coerce argument. This validates that the given values exist in the list of choices and can be coerced.

Syntax: TypedMultipleChoiceField(**kwargs)

Example:

from django import forms

class SelectMultipleForm(forms.Form):
    typed_multiple_choice_field = forms.TypedMultipleChoiceField(label='Typed Multiple Choice Field', label_suffix=" : ",
                                                                required=True, disabled=False, 
                                                                coerce=str, choices=city,
                                                                widget=forms.SelectMultiple(
                                                                    attrs={'class': 'form-control'}),
                                                                error_messages={'required': "This field is required."})

Let’s run the server and check the result.

Rendered HTML

Explanation:

Field OptionsDescription
labelThis argument allows you to specify a “Field Caption” label for this area.
label_suffixThis argument allows you to insert some text after the field’s label.
requiredBy default, each field assumes a value as required, so you need to set the required as a false to make it not necessary.
widgetThis Argument lets you specify widget classes to use when presenting this field. So you can use bootstrap class or any other CSS class to decorate your field. Also, you can pass the choice list.
For eg. typed_multiple_choice_field= forms.TypedMultipleChoiceField(widget=forms.SelectMultiple(attrs={‘class’: ‘form-control’}))
error_messagesThis argument allows you to overwrite the default error message that the field will raise. Pass the error message to overwrite in the dictionary with the matching key.
disabledDisabled Boolean Argument, if set to true, disables the form field using the disabled HTML attribute so that it cannot be edited by users.

See the official site for more information.

I hope you understand what I explained about SelectMultiple Django Form Fields in this blog. If you have any queries regarding the SelectMultiple Django Form Fields, leave a comment below.

Find the source code for SelectMultiple Django Form Fields(MultipleChoiceField, TypedMultipleChoiceField) Github link at:

https://github.com/pranalikambli/django_form_fields

thank you

Leave a Comment