Package: django-enum
Django EnumFull and natural support forenumerationsas Django model fields.Many packages aim to ease usage of Python enumerations as model fields. Most
were made obsolete when Django providedTextChoicesandIntegerChoicestypes. The motivation fordjango-enumwas to:Always automatically coerce fields to instances of the Enum type.Allow strict adherence to Enum values to be disabled.Be compatible with Enum classes that do not derive from Django’s Choices.Handle migrations appropriately. (Seemigrations)Integrate as fully as possible withDjango’s existing level of enum support.Integrate withenum-propertiesto enable richer enumeration types.Represent enum fields with the smallest possible column type.Be as simple and light-weight an extension to core Django as possible.django-enumworks in concert
withDjango’s built inTextChoicesandIntegerChoicesto provide a
new model field type,EnumField, that resolves the correct nativeDjangofield type for the given enumeration based on its value type and range. For
example,IntegerChoicesthat contain values between 0 and 32767 becomePositiveSmallIntegerField.fromdjango.dbimportmodelsfromdjango_enumimportEnumFieldclassMyModel(models.Model):classTextEnum(models.TextChoices):VALUE0='V0','Value 0'VALUE1='V1','Value 1'VALUE2='V2','Value 2'classIntEnum(models.IntegerChoices):ONE=1,'One'TWO=2,'Two',THREE=3,'Three'# this is equivalent to:#  CharField(max_length=2, choices=TextEnum.choices, null=True, blank=True)txt_enum=EnumField(TextEnum,null=True,blank=True)# this is equivalent to#  PositiveSmallIntegerField(choices=IntEnum.choices)int_enum=EnumField(IntEnum)EnumFieldis more than just an alias. The fields are now assignable and
accessible as their enumeration type rather than by-value:instance=MyModel.objects.create(txt_enum=MyModel.TextEnum.VALUE1,int_enum=3# by-value assignment also works)assertinstance.txt_enum==MyModel.TextEnum('V1')assertinstance.txt_enum.label=='Value 1'assertinstance.int_enum==MyModel.IntEnum['THREE']assertinstance.int_enum.value==3django-enumalso providesIntegerChoicesandTextChoicestypes that extend fromenum-propertieswhich makes
possible very rich enumeration fields.?>pip installenum-propertiesfromenum_propertiesimportsfromdjango_enumimportTextChoices# use instead of Django's TextChoicesfromdjango.dbimportmodelsclassTextChoicesExample(models.Model):classColor(TextChoices,s('rgb'),s('hex',case_fold=True)):# name   value   label       rgb       hexRED='R','Red',(1,0,0),'ff0000'GREEN='G','Green',(0,1,0),'00ff00'BLUE='B','Blue',(0,0,1),'0000ff'# any named s() values in the Enum's inheritance become properties on# each value, and the enumeration value may be instantiated from the# property's valuecolor=EnumField(Color)instance=TextChoicesExample.objects.create(color=TextChoicesExample.Color('FF0000'))assertinstance.color==TextChoicesExample.Color('Red')assertinstance.color==TextChoicesExample.Color('R')assertinstance.color==TextChoicesExample.Color((1,0,0))# direct comparison to any symmetric value also worksassertinstance.color=='Red'assertinstance.color=='R'assertinstance.color==(1,0,0)# save by any symmetric valueinstance.color='FF0000'# access any enum property right from the model fieldassertinstance.color.hex=='ff0000'# this also works!assertinstance.color=='ff0000'# and so does this!assertinstance.color=='FF0000'instance.save()# filtering works by any symmetric value or enum type instanceassertTextChoicesExample.objects.filter(color=TextChoicesExample.Color.RED).first()==instanceassertTextChoicesExample.objects.filter(color=(1,0,0)).first()==instanceassertTextChoicesExample.objects.filter(color='FF0000').first()==instanceNoteConsider usingdjango-render-staticto make your enumerationsDRYacross the full stack!Please report bugs and discuss features on theissues page.Contributionsare encouraged!Full documentation at read the docs.InstallationClone django-enum fromGitHubor install a release offPyPI:pipinstalldjango-enumNotedjango-enumhas several optional dependencies that are not pulled in
by default.EnumFieldswork seamlessly with all Django apps that
work with model fields with choices without any additional work. Optional
integrations are provided with several popular libraries to extend this
basic functionality.Integrations are provided that leverageenum-propertiesto make
enumerations do more work and to provide extended functionality fordjango-filteranddjangorestframework.pipinstallenum-propertiespipinstalldjango-filterpipinstalldjangorestframeworkIf features are utilized that require a missing optional dependency an
exception will be thrown.