EnumChoiceField¶
-
class
enumchoicefield.fields.EnumChoiceField(enum_class, ...)¶ Create an EnumChoiceField. This field generates choices from an
enum.Enum.The
EnumChoiceFieldextendsdjango.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
nameattribute. This keeps the database representation stable when adding and removing enum members.A
max_lengthis automatically generated from the longestname. If you add a new enum member with a longer name, or remove the longest member, the generatedmax_lengthwill change. To prevent this, you can manually set amax_lengthargument, and this will be used instead.If a default choice is supplied, the enum class must have a
deconstructmethod. If the enum inherits fromDeconstructableEnum, this will be handled for you.The display value for the Enums is taken from the
strrepresentation of each value. By default this is something likeMyEnum.foo, which is not very user friendly.PrettyEnummakes defining a human-readablestrrepresentation easy.