Form Fields¶
- class django_enum.forms.ChoiceFieldMixin(enum: type[Enum] | None = None, primitive: type | None = None, *, empty_value: Any = <class 'django_enum.forms._Unspecified'>, strict: bool = True, empty_values: list[Any] | type[_Unspecified] = <class 'django_enum.forms._Unspecified'>, choices: Any, ~typing.Any] | tuple[str, ~typing.Iterable[tuple[~typing.Any, ~typing.Any]]]] | ~django_enum.forms._ChoicesCallable=(), coerce: _CoerceCallable | None = None, **kwargs)[source]¶
Bases:
objectMixin to adapt
django.forms.ChoiceFieldto use withEnumField.- Parameters:
enum – The Enumeration type
empty_value – Allow users to define what empty is because some enumeration types might use an empty value (i.e. empty string) as an enumeration value. This value will be returned when any “empty” value is encountered. If unspecified the default empty value of ‘’ is returned.
empty_values – Override the list of what are considered to be empty values. Defaults to TypedChoiceField.empty_values.
strict – If False, values not included in the enumeration list, but of the same primitive type are acceptable.
choices – Override choices, otherwise enumeration choices attribute will be used.
kwargs – Any additional parameters to pass to ChoiceField base class.
- __init__(enum: type[Enum] | None = None, primitive: type | None = None, *, empty_value: Any = <class 'django_enum.forms._Unspecified'>, strict: bool = True, empty_values: list[Any] | type[_Unspecified] = <class 'django_enum.forms._Unspecified'>, choices: Any, ~typing.Any] | tuple[str, ~typing.Iterable[tuple[~typing.Any, ~typing.Any]]]] | ~django_enum.forms._ChoicesCallable=(), coerce: _CoerceCallable | None = None, **kwargs)[source]¶
- default_coerce(value: Any) Any[source]¶
Attempt conversion of value to an enumeration value and return it if successful.
Note
When used to represent a model field, by default the model field’s to_python method will be substituted for this method.
- Parameters:
value – The value to convert
- Raises:
ValidationError – if a valid return value cannot be determined.
- Returns:
An enumeration value or the canonical empty value if value is one of our empty_values, or the value itself if this is a non-strict field and the value is of a matching primitive type
- property enum¶
the class of the enumeration
- non_strict_widget¶
alias of
NonStrictSelect
- property primitive¶
The most appropriate primitive non-Enumeration type that can represent all enumeration values.
- property strict¶
strict fields allow non-enumeration values
- class django_enum.forms.EnumChoiceField(enum: type[Enum] | None = None, primitive: type | None = None, *, empty_value: Any = <class 'django_enum.forms._Unspecified'>, strict: bool = True, empty_values: list[Any] | type[_Unspecified] = <class 'django_enum.forms._Unspecified'>, choices: Any, ~typing.Any] | tuple[str, ~typing.Iterable[tuple[~typing.Any, ~typing.Any]]]] | ~django_enum.forms._ChoicesCallable=(), coerce: _CoerceCallable | None = None, **kwargs)[source]¶
Bases:
ChoiceFieldMixin,TypedChoiceFieldThe default
ChoiceFieldwill only accept the base enumeration values. Use this field on forms to accept any value mappable to an enumeration including any labels, symmetric properties, of values accepted in_missing_().Tip
See
ChoiceFieldMixinfor the list of parameters accepted by the form fields. These parameters mirror the parameters forEnumField.
- class django_enum.forms.EnumFlagField(enum: type[Flag] | None = None, *, empty_value: Any = <class 'django_enum.forms._Unspecified'>, strict: bool = True, empty_values: list[Any] | type[_Unspecified] = <class 'django_enum.forms._Unspecified'>, choices: Any, ~typing.Any] | tuple[str, ~typing.Iterable[tuple[~typing.Any, ~typing.Any]]]] | ~django_enum.forms._ChoicesCallable=(), **kwargs)[source]¶
Bases:
ChoiceFieldMixin,TypedMultipleChoiceFieldA generic form field for
Flagderived enumerations. By default theFlagSelectMultiplewidget will be used.After cleaning the value stored in the cleaned data will be a combined enum instance. (e.g. all input flags will be or-ed together)
Note
The default empty_value is Flag(0) but when used in a ModelForm the empty_value will be automatically set to None if null=True.
- __init__(enum: type[Flag] | None = None, *, empty_value: Any = <class 'django_enum.forms._Unspecified'>, strict: bool = True, empty_values: list[Any] | type[_Unspecified] = <class 'django_enum.forms._Unspecified'>, choices: Any, ~typing.Any] | tuple[str, ~typing.Iterable[tuple[~typing.Any, ~typing.Any]]]] | ~django_enum.forms._ChoicesCallable=(), **kwargs)[source]¶
- non_strict_widget¶
alias of
NonStrictFlagSelectMultiple
- widget¶
alias of
FlagSelectMultiple
- class django_enum.forms.EnumMultipleChoiceField(enum: type[Enum] | None = None, primitive: type | None = None, *, empty_value: Any = <class 'django_enum.forms._Unspecified'>, strict: bool = True, empty_values: list[Any] | type[_Unspecified] = <class 'django_enum.forms._Unspecified'>, choices: Any, ~typing.Any] | tuple[str, ~typing.Iterable[tuple[~typing.Any, ~typing.Any]]]] | ~django_enum.forms._ChoicesCallable=(), coerce: _CoerceCallable | None = None, **kwargs)[source]¶
Bases:
ChoiceFieldMixin,TypedMultipleChoiceFieldThe default
MultipleChoiceFieldwill only accept the base enumeration values. Use this field on forms to accept multiple values mappable to an enumeration including any labels, symmetric properties, of values accepted in_missing_().- non_strict_widget¶
alias of
NonStrictSelectMultiple