Registry / web-framework / django-enumfields

django-enumfields

JSON →
library2.1.1pypypiunverified

django-enumfields provides robust integration of Python's `enum.Enum` types with Django models, forms, and REST Framework serializers. It's currently at version 2.1.1 and follows a moderate release cadence, primarily focusing on Django and Python version compatibility and bug fixes.

pip install django-enumfields
INSTALL
IMPORT
SIG · DJANGO-ENUMFIELDS
D
django-enumfields
web-frameworkpythonv2.1.1
Install
1.6s avg
Import
—
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
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
musl
py 3.10–3.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 17.9MB
glibc
py 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
Debug
Known issues
breakingVersion 2.0.0 dropped support for Python 2.7, 3.4 and Django 1.8, 1.10. Ensure your project meets the new minimum requirements.
fix
Upgrade Python to 3.7+ and Django to 2.1+ before upgrading `django-enumfields` to 2.0.0 or later.
affects: <2.0.0
breakingVersion 2.1.0 dropped support for Django 2.0 and 2.1. Projects on these Django versions must upgrade Django or stick to older `django-enumfields` versions.
fix
Upgrade Django to 2.2, 3.0, 3.1 or newer. If not possible, pin `django-enumfields<2.1.0`.
affects: >=2.1.0
gotchaA regression in version 2.1.0 mistakenly broke string-to-enum conversion, potentially causing `ValueError` or incorrect data when loading models or deserializing data where string values were expected.
fix
Immediately upgrade to `django-enumfields==2.1.1` or newer to fix the string conversion issue.
affects: 2.1.0
gotcha`EnumCharField` requires `max_length` just like a standard Django `CharField`. Forgetting it will result in `FieldError` or `SystemCheckError`.
fix
Always specify `max_length` when using `EnumCharField`, e.g., `EnumCharField(MyCharEnum, max_length=10)`.
affects: all
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.
Agent activity
8 hits · last 30 days
node
8
Resources
django-enumfields — pip install django-enumfields · libregistry