Skip to content

rogerhu/django-richenum

 
 

Repository files navigation

django-richenum

Build Status Latest PyPI Version

About

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.

Model Fields

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.

Form Fields

CanonicalEnumField
Uses the RichEnum/OrderedRichEnum canonical_name as form field values.
IndexEnumField
Uses the OrderedRichEnum index as form field values.

Links

Installation

$ pip install django-richenum

Example Usage

IndexEnumField

>>> 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)

CanonicalNameEnumField

>>> 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)

Related Packages

richenum

Package implementing RichEnum and OrderedRichEnum that django-richenum depends on.

Contributing

  1. Fork the repo from GitHub.
  2. Make your changes.
  3. Add unittests for your changes.
  4. Run pep8, pyflakes, and pylint to make sure your changes follow the Python style guide and doesn't have any errors.
  5. Add yourself to the AUTHORS file (in alphabetical order).
  6. Send a pull request from your fork to the main repo.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 100.0%