EnumChoiceField¶
-
class
enumchoicefield.fields.
EnumChoiceField
(enum_class, ...)¶ Create an EnumChoiceField. This field generates choices from an
enum.Enum
.The
EnumChoiceField
extendsdjango.db.models.Field
. It accepts one additional argument:enum_class
, which should be a subclass ofenum.Enum
. It is recommended that this enum subclassesChoiceEnum
, but this is not required.When saving enum members to the database, The chosen member is stored in the database using its
name
attribute. This keeps the database representation stable when adding and removing enum members.A
max_length
is automatically generated from the longestname
. If you add a new enum member with a longer name, or remove the longest member, the generatedmax_length
will change. To prevent this, you can manually set amax_length
argument, and this will be used instead.If a default choice is supplied, the enum class must have a
deconstruct
method. If the enum inherits fromDeconstructableEnum
, this will be handled for you.The display value for the Enums is taken from the
str
representation of each value. By default this is something likeMyEnum.foo
, which is not very user friendly.PrettyEnum
makes defining a human-readablestr
representation easy.