Install & Compatibility
Where this runs
tested against v2.1.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 17.9MB
glibcpy 3.10–3.910 runs
installs and imports cleanly · install 1.6s · import 0.000s · 18MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
EnumField
✓ from enumfields import EnumField
✗ from enumfields import EnumField
EnumIntegerField
✓ from enumfields import EnumIntegerField
EnumFieldMixin
✓ from enumfields import EnumFieldMixin
Define Python `enum.Enum` classes and use `EnumField`, `EnumIntegerField`, or `EnumCharField` in your Django models. The field stores the enum's value in the database and retrieves it as the enum instance.
import enum
from django.db import models
from enumfields import EnumField, EnumIntegerField, EnumCharField
class MyEnum(enum.Enum):
FIRST = 1
SECOND = 2
THIRD = 3
class MyCharEnum(enum.Enum):
FOO = 'foo'
BAR = 'bar'
class MyModel(models.Model):
# Stores the enum value as its internal integer value
my_int_field = EnumIntegerField(MyEnum, default=MyEnum.FIRST)
# Stores the enum value as its internal value (can be mixed types)
my_field = EnumField(MyEnum, default=MyEnum.FIRST)
# Stores the enum value as a string (requires max_length)
my_char_field = EnumCharField(MyCharEnum, max_length=10, default=MyCharEnum.FOO)
def __str__(self):
return f'{self.my_int_field.name} - {self.my_char_field.value}'
# Example usage:
# instance = MyModel.objects.create(my_int_field=MyEnum.SECOND, my_char_field=MyCharEnum.BAR)
# print(instance.my_int_field) # MyEnum.SECOND
# print(instance.my_int_field.value) # 2
# print(instance.my_char_field.name) # FOO
Upgrade
Version history
2.1.1latest on PyPI · released Feb 23, 2021
Audit
Dependencies
DjangorequiredCore functionality is built on Django's ORM.
djangorestframeworkoptionalProvides EnumField serializer support for DRF.