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: object

Mixin to adapt django.forms.ChoiceField to use with EnumField.

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

prepare_value(value: Any) Any[source]

Must return the raw enumeration value type

property primitive

The most appropriate primitive non-Enumeration type that can represent all enumeration values.

property strict

strict fields allow non-enumeration values

to_python(value: Any) Any[source]

Return the value as its full enumeration object

valid_value(value: Any) bool[source]

Return false if this value is not valid

validate(value)[source]

Validate that the input is in self.choices.

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, TypedChoiceField

The default ChoiceField will 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 ChoiceFieldMixin for the list of parameters accepted by the form fields. These parameters mirror the parameters for EnumField.

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, TypedMultipleChoiceField

A generic form field for Flag derived enumerations. By default the FlagSelectMultiple widget 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]
has_changed(initial, data)[source]

Return True if data differs from initial.

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, TypedMultipleChoiceField

The default MultipleChoiceField will 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_().

has_changed(initial, data)[source]

Return True if data differs from initial.

non_strict_widget

alias of NonStrictSelectMultiple