A Django extension of richenum for Python. If you're unfamiliar with richenums, please read up on them (see Related Packages) before using django-richenum.
- IndexEnumField
- Store ints in DB, but expose OrderedRichEnumValues in Python.
- CanonicalNameEnumField
- Store varchar in DB, but expose RichEnumValues in Python. We recommend that you use IndexEnumField for storage and query efficiency.
- LaxIndexEnumField
- Like IndexEnumField, but also allows casting to and from canonical names. Mainly used to help migrate existing code that uses strings as database values.
- CanonicalEnumField
- Uses the RichEnum/OrderedRichEnum canonical_name as form field values.
- IndexEnumField
- Uses the OrderedRichEnum index as form field values.
- RichEnumFieldListFilter
- Enables filtering by RichEnum model fields in the Django admin UI
$ pip install django-richenum
>>> from richenum import OrderedRichEnum, OrderedRichEnumValue
>>> class MyOrderedRichEnum(OrderedRichEnum):
... FOO = OrderedRichEnumValue(index=1, canonical_name="foo", display_name="Foo")
... BAR = OrderedRichEnumValue(index=2, canonical_name="bar", display_name="Bar")
...
>>> from django.db import models
>>> from django_richenum.models import IndexEnumField
>>> class MyModel(models.Model):
... my_enum = IndexEnumField(MyOrderedRichEnum, default=MyOrderedRichEnum.FOO)
...
>>> m = MyModel.objects.create(my_enum=MyOrderedRichEnum.BAR)
>>> m.save()
>>> m.my_enum
OrderedRichEnumValue - idx: 2 canonical_name: 'bar' display_name: 'Bar'
>>> MyModel.objects.filter(my_enum=MyOrderedRichEnum.BAR)
>>> from richenum import RichEnum, RichEnumValue
>>> class MyRichEnum(RichEnum):
... FOO = RichEnumValue(canonical_name="foo", display_name="Foo")
... BAR = RichEnumValue(canonical_name="bar", display_name="Bar")
...
>>> from django.db import models
>>> from django_richenum.models import CanonicalNameEnumField
>>> class MyModel(models.Model):
... my_enum = CanonicalNameEnumField(MyRichEnum, default=MyRichEnum.FOO)
...
>>> m = MyModel.objects.create(my_enum=MyRichEnum.BAR)
>>> m.save()
>>> m.my_enum
RichEnumValue - canonical_name: 'bar' display_name: 'Bar'
>>> MyModel.objects.filter(my_enum=MyRichEnum.BAR)
>>> from django_richenum.admin import register_admin_filters
>>> register_admin_filters()
- richenum
Package implementing RichEnum and OrderedRichEnum that django-richenum depends on.
If you're using Django 1.7+, you'll need to use the @deconstructible
decorator for your RichEnumValue
and OrderedRichEnumValue
classes so Django's migration framework knows how to serialize your RichEnumValue
and OrderedRichEnumValue
.
>>> from django.utils.deconstruct import deconstructible
>>> from richenum import RichEnumValue, OrderedRichEnumValue
>>> @deconstructible
... class CustomRichEnumValue(RichEnumValue):
... pass
...
>>> @deconstructible
... class CustomOrderedRichEnumValue(OrderedRichEnumValue):
... pass
...
- Fork the repo from GitHub.
- Make your changes.
- Add unittests for your changes.
- Run pep8, pyflakes, and pylint to make sure your changes follow the Python style guide and doesn't have any errors.
- Add yourself to the AUTHORS file (in alphabetical order).
- Send a pull request from your fork to the main repo.